Refactor app2sd server codes 84/144684/5
authorSangyoon Jang <jeremy.jang@samsung.com>
Wed, 16 Aug 2017 11:16:45 +0000 (20:16 +0900)
committerSangyoon Jang <jeremy.jang@samsung.com>
Fri, 18 Aug 2017 06:20:39 +0000 (15:20 +0900)
- Fix code style
- Remove duplicated codes
- Remove unnecessary codes
- Fix indentations

Change-Id: I85d9f2db34182f669030f3c60d5cb98c7c77dbf5
Signed-off-by: Sangyoon Jang <jeremy.jang@samsung.com>
plugin/app2sd/server/app2sd_interface.c
plugin/app2sd/server/app2sd_internals.c
plugin/app2sd/server/app2sd_internals.h
plugin/app2sd/server/app2sd_internals_registry.c
plugin/app2sd/server/app2sd_internals_utils.c
plugin/app2sd/server/app2sd_server.c

index 11888fe..c4821f3 100644 (file)
 
 static int __app2sd_create_app2sd_directories(uid_t uid, char *mmc_path)
 {
-       int ret = 0;
-       char app2sd_path[FILENAME_MAX] = { 0, };
+       int ret;
+       char path[FILENAME_MAX];
        mode_t mode = DIR_PERMS;
 
-       snprintf(app2sd_path, sizeof(app2sd_path), "%s/%s",
-               mmc_path, EXTIMG_DIR);
-       ret = mkdir(app2sd_path, mode);
+       snprintf(path, sizeof(path), "%s/%s", mmc_path, EXTIMG_DIR);
+       ret = mkdir(path, mode);
        if (ret) {
                if (errno != EEXIST) {
-                       _E("create directory failed," \
-                               " error no is (%d)", errno);
+                       _E("create directory failed, error is (%d)", errno);
                        return APP2EXT_ERROR_CREATE_DIRECTORY;
                }
        }
@@ -48,22 +46,21 @@ static int __app2sd_create_app2sd_directories(uid_t uid, char *mmc_path)
 }
 
 static int __app2sd_finalize_device_setup(const char *pkgid,
-       const char *loopback_device, const char *application_path,
-       uid_t uid)
+               const char *loopback_device, const char *application_path,
+               uid_t uid)
 {
-       char *device_node = NULL;
-       int ret = APP2EXT_SUCCESS;
+       char *device_node;
+       int ret;
 
 #ifdef TIZEN_FEATURE_APP2SD_DMCRYPT_ENCRYPTION
-       device_node =
-               _app2sd_find_associated_dmcrypt_device_node(pkgid, uid);
+       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) {
+       if (!device_node) {
                _E("failed to find device node");
                return APP2EXT_ERROR_FIND_ASSOCIATED_DEVICE_NODE;
        }
@@ -71,59 +68,46 @@ static int __app2sd_finalize_device_setup(const char *pkgid,
 
        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);
+               free(device_node);
                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);
+               free(device_node);
                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");
+               _E("unable to detach the loopback encryption setup "
+                               "for the application");
+               free(device_node);
                return APP2EXT_ERROR_UNMOUNT;
        }
 #endif
-
-       if (device_node) {
-               free(device_node);
-               device_node = NULL;
-       }
+       free(device_node);
 
        return ret;
 }
 
-int app2sd_usr_pre_app_install(const char *pkgid, GList *dir_list, int size, uid_t uid)
+int app2sd_usr_pre_app_install(const char *pkgid, GList *dir_list, int size,
+               uid_t uid)
 {
-       int ret = 0;
-       int free_mmc_mem = 0;
-       char *sdpath = NULL;
-       char *device_node = NULL;
+       int ret;
+       int free_mmc_mem;
+       char *sdpath;
+       char *device_node;
 #if !defined(TIZEN_FEATURE_APP2SD_DMCRYPT_ENCRYPTION)
-       char *devi = NULL;
-       char *result = NULL;
+       char *devi;
+       char *result;
 #endif
-       char mmc_path[FILENAME_MAX] = { 0, };
-       char application_path[FILENAME_MAX] = { 0, };
-       char loopback_device[FILENAME_MAX] = { 0, };
-       char *encoded_id = NULL;
+       char mmc_path[FILENAME_MAX];
+       char application_path[FILENAME_MAX];
+       char loopback_device[FILENAME_MAX];
        int reqd_disk_size = size + ceil(size * 0.2);
 
        /* validate the function parameter recieved */
@@ -148,9 +132,9 @@ int app2sd_usr_pre_app_install(const char *pkgid, GList *dir_list, int size, uid
                _E("unable to get available free memory in MMC (%d)", ret);
                return APP2EXT_ERROR_MMC_STATUS;
        }
-       _D("size details for application installation:" \
-               " size=(%d)MB, reqd_disk_size=(%d)MB, free_mmc_size=(%d)MB",
-               size, reqd_disk_size, free_mmc_mem);
+       _D("size details for application installation: size=(%d)MB, "
+                       "reqd_disk_size=(%d)MB, free_mmc_size=(%d)MB",
+                       size, reqd_disk_size, free_mmc_mem);
 
        /* if avaialalbe free memory in MMC is less than required size + 5MB,
         * return error
@@ -161,15 +145,10 @@ int app2sd_usr_pre_app_install(const char *pkgid, GList *dir_list, int size, uid
                return APP2EXT_ERROR_MMC_INSUFFICIENT_MEMORY;
        }
 
-       encoded_id = _app2sd_get_encoded_name(pkgid, uid);
-       if (encoded_id == NULL)
-               return APP2EXT_ERROR_MEMORY_ALLOC_FAILED;
-       snprintf(loopback_device, sizeof(loopback_device), "%s/%s/%s",
-                       mmc_path, EXTIMG_DIR, encoded_id);
-       free(encoded_id);
-
-       _app2sd_set_application_path(pkgid, uid, application_path,
-               sizeof(application_path));
+       ret = _app2sd_get_loopback_device_path(mmc_path, pkgid, uid,
+                       loopback_device, sizeof(loopback_device));
+       if (ret)
+               return ret;
 
        ret = __app2sd_create_app2sd_directories(uid, mmc_path);
        if (ret) {
@@ -179,7 +158,7 @@ int app2sd_usr_pre_app_install(const char *pkgid, GList *dir_list, int size, uid
 
        /* create a loopback device */
        ret = _app2sd_create_loopback_device(pkgid, loopback_device,
-               (reqd_disk_size + PKG_BUF_SIZE));
+                       (reqd_disk_size + PKG_BUF_SIZE));
 
 #ifdef TIZEN_FEATURE_APP2SD_DMCRYPT_ENCRYPTION
        ret = _app2sd_dmcrypt_setup_device(pkgid, loopback_device, false, uid);
@@ -197,7 +176,7 @@ int app2sd_usr_pre_app_install(const char *pkgid, GList *dir_list, int size, uid
 #else
        /* perform loopback encryption setup */
        device_node = _app2sd_do_loopback_encryption_setup(pkgid,
-               loopback_device, uid);
+                       loopback_device, uid);
        if (!device_node) {
                _E("loopback encryption setup failed");
                _app2sd_delete_loopback_device(loopback_device);
@@ -226,9 +205,12 @@ int app2sd_usr_pre_app_install(const char *pkgid, GList *dir_list, int size, uid
        /* mount the loopback encrypted pseudo device on application
         * installation path as with Read Write permission
         */
+
+       _app2sd_set_application_path(pkgid, uid, application_path,
+                       sizeof(application_path));
        ret = _app2sd_mount_app_content(application_path, pkgid,
-               device_node, MOUNT_TYPE_RW, dir_list,
-               APP2SD_PRE_INSTALL, uid);
+                       device_node, MOUNT_TYPE_RW, dir_list,
+                       APP2SD_PRE_INSTALL, uid);
        if (ret) {
                _E("mounting dev path to app install path failed");
                ret = APP2EXT_ERROR_MOUNT_PATH;
@@ -242,31 +224,25 @@ int app2sd_usr_pre_app_install(const char *pkgid, GList *dir_list, int size, uid
 FINISH_OFF:
        if (device_node) {
 #ifdef TIZEN_FEATURE_APP2SD_DMCRYPT_ENCRYPTION
-       ret = _app2sd_dmcrypt_close_device(pkgid, uid);
-       if (ret)
-               _E("close dmcrypt device error(%d)", ret);
-       _app2sd_delete_loopback_device(loopback_device);
+               ret = _app2sd_dmcrypt_close_device(pkgid, uid);
+               if (ret)
+                       _E("close dmcrypt device error(%d)", ret);
+               _app2sd_delete_loopback_device(loopback_device);
 #else
                result = _app2sd_detach_loop_device(device_node);
-               if (result) {
+               if (result)
                        free(result);
-                       result = NULL;
-               }
                _app2sd_delete_loopback_device(loopback_device);
 #endif
        }
 
 END:
-       if (device_node) {
+       if (device_node)
                free(device_node);
-               device_node = NULL;
-       }
 
 #if !defined(TIZEN_FEATURE_APP2SD_DMCRYPT_ENCRYPTION)
-       if (devi) {
+       if (devi)
                free(devi);
-               devi = NULL;
-       }
 #endif
 
        sync();
@@ -276,18 +252,17 @@ END:
 int app2sd_usr_post_app_install(const char *pkgid,
                app2ext_status install_status, uid_t uid)
 {
-       char *sdpath = NULL;
-       char mmc_path[FILENAME_MAX] = { 0, };
-       char application_path[FILENAME_MAX] = { 0, };
-       char app_mmc_path[FILENAME_MAX] = { 0, };
-       char loopback_device[FILENAME_MAX] = { 0, };
-       char *encoded_id = NULL;
-       int ret = APP2EXT_SUCCESS;
-       int pkgmgr_ret = 0;
+       char *sdpath;
+       char mmc_path[FILENAME_MAX];
+       char application_path[FILENAME_MAX];
+       char app_mmc_path[FILENAME_MAX];
+       char loopback_device[FILENAME_MAX];
+       int ret;
+       int pkgmgr_ret;
 
        /* validate the function parameter recieved */
-       if (pkgid == NULL || install_status < APP2EXT_STATUS_FAILED
-               || install_status > APP2EXT_STATUS_SUCCESS) {
+       if (pkgid == NULL || install_status < APP2EXT_STATUS_FAILED ||
+                       install_status > APP2EXT_STATUS_SUCCESS) {
                _E("invalid func parameters");
                return APP2EXT_ERROR_INVALID_ARGUMENTS;
        }
@@ -302,20 +277,18 @@ int app2sd_usr_post_app_install(const char *pkgid,
        free(sdpath);
        sync();
 
-       encoded_id = _app2sd_get_encoded_name(pkgid, uid);
-       if (encoded_id == NULL)
-               return APP2EXT_ERROR_MEMORY_ALLOC_FAILED;
-       snprintf(loopback_device, sizeof(loopback_device), "%s/%s/%s",
-               mmc_path, EXTIMG_DIR, encoded_id);
-       free(encoded_id);
+       ret = _app2sd_get_loopback_device_path(mmc_path, pkgid, uid,
+                       loopback_device, sizeof(loopback_device));
+       if (ret)
+               return ret;
 
        _app2sd_set_application_path(pkgid, uid, application_path,
-               sizeof(application_path));
+                       sizeof(application_path));
        snprintf(app_mmc_path, sizeof(app_mmc_path), "%s/.mmc",
-               application_path);
+                       application_path);
 
        ret = __app2sd_finalize_device_setup(pkgid, loopback_device,
-               application_path, uid);
+                       application_path, uid);
        if (ret) {
                _E("failed to finalize device setup");
                return ret;
@@ -328,7 +301,8 @@ int app2sd_usr_post_app_install(const char *pkgid,
                /* delete the loopback device from the SD card */
                ret = _app2sd_delete_loopback_device(loopback_device);
                if (ret) {
-                       _E("unable to delete the loopback device from the SD Card");
+                       _E("unable to delete the loopback device from "
+                                       "the SD Card");
                        return APP2EXT_ERROR_DELETE_LOOPBACK_DEVICE;
                }
                ret = _app2sd_remove_info_from_db(pkgid, uid);
@@ -337,7 +311,8 @@ int app2sd_usr_post_app_install(const char *pkgid,
 
                ret = _app2sd_delete_directory(app_mmc_path);
                if (ret)
-                       _E("unable to delete the directory (%s)", application_path);
+                       _E("unable to delete the directory (%s)",
+                                       application_path);
        } else {
                /* if the status is success, then update installed storage
                 * to pkgmgr_parser db
@@ -345,9 +320,10 @@ int app2sd_usr_post_app_install(const char *pkgid,
                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);
+                       _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;
                }
        }
@@ -358,17 +334,16 @@ int app2sd_usr_post_app_install(const char *pkgid,
 
 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;
+       int ret;
+       char mmc_path[FILENAME_MAX];
+       char application_path[FILENAME_MAX];
+       char loopback_device[FILENAME_MAX];
+       char *sdpath;
+       char *device_node;
 #if !defined(TIZEN_FEATURE_APP2SD_DMCRYPT_ENCRYPTION)
-       char *result = NULL;
+       char *result;
 #endif
-       FILE *fp = NULL;
+       FILE *fp;
 
        /* validate the function parameter recieved */
        if (pkgid == NULL) {
@@ -385,19 +360,17 @@ int app2sd_usr_on_demand_setup_init(const char *pkgid, uid_t uid)
        snprintf(mmc_path, sizeof(mmc_path), "%s", sdpath);
        free(sdpath);
 
-       encoded_id = _app2sd_get_encoded_name(pkgid, uid);
-       if (encoded_id == NULL)
-               return APP2EXT_ERROR_MEMORY_ALLOC_FAILED;
-       snprintf(loopback_device, sizeof(loopback_device), "%s/%s/%s",
-                       mmc_path, EXTIMG_DIR, encoded_id);
-       free(encoded_id);
+       ret = _app2sd_get_loopback_device_path(mmc_path, pkgid, uid,
+                       loopback_device, sizeof(loopback_device));
+       if (ret)
+               return ret;
        _app2sd_set_application_path(pkgid, uid, application_path,
-               sizeof(application_path));
+                       sizeof(application_path));
 
        fp = fopen(loopback_device, "r+");
        if (fp == NULL) {
                _E("app entry is not present in SD Card, (%s), errno(%d)",
-                       loopback_device, errno);
+                               loopback_device, errno);
                return APP2EXT_ERROR_INVALID_PACKAGE;
        }
        fclose(fp);
@@ -412,7 +385,7 @@ int app2sd_usr_on_demand_setup_init(const char *pkgid, uid_t uid)
        }
 
        ret = _app2sd_dmcrypt_open_device(pkgid, loopback_device,
-               false, uid, &device_node);
+                       false, uid, &device_node);
        if (ret) {
                _E("dmcrypt open device error(%d)", ret);
                return APP2EXT_ERROR_OPEN_DMCRYPT_DEVICE;
@@ -423,13 +396,12 @@ int app2sd_usr_on_demand_setup_init(const char *pkgid, uid_t uid)
        if ((result != NULL) && strstr(result, "/dev") != NULL) {
                _E("already associated");
                free(result);
-               result = NULL;
                return APP2EXT_ERROR_ALREADY_MOUNTED;
        }
 
        /* do loopback setup */
        device_node = _app2sd_do_loopback_encryption_setup(pkgid,
-               loopback_device, uid);
+                       loopback_device, uid);
        if (device_node == NULL) {
                _E("loopback encryption setup failed");
                return APP2EXT_ERROR_DO_LOSETUP;
@@ -437,30 +409,25 @@ int app2sd_usr_on_demand_setup_init(const char *pkgid, uid_t uid)
 #endif
 
        /* do mounting */
-       ret = _app2sd_mount_app_content(application_path, pkgid,
-               device_node, MOUNT_TYPE_RD, NULL, APP2SD_APP_LAUNCH, uid);
+       ret = _app2sd_mount_app_content(application_path, pkgid, device_node,
+                       MOUNT_TYPE_RD, NULL, APP2SD_APP_LAUNCH, uid);
        if (ret) {
                _E("mount failed");
-               if (device_node) {
-                       free(device_node);
-                       device_node = NULL;
-               }
+               free(device_node);
                return APP2EXT_ERROR_MOUNT_PATH;
        }
 
-       if (device_node) {
-               free(device_node);
-               device_node = NULL;
-       }
+       free(device_node);
 
        return ret;
 }
 
-static int _app2sd_application_handler(const pkgmgrinfo_appinfo_h handle, void *data)
+static int _app2sd_application_handler(const pkgmgrinfo_appinfo_h handle,
+               void *data)
 {
-       int ret = 0;
-       int pid = 0;
-       char *appid = NULL;
+       int ret;
+       int pid;
+       char *appid;
        uid_t uid = *(uid_t *)data;
 
        ret = pkgmgrinfo_appinfo_get_appid(handle, &appid);
@@ -492,7 +459,7 @@ static int _app2sd_application_handler(const pkgmgrinfo_appinfo_h handle, void *
 
 static int _app2sd_kill_running_app(const char *pkgid, uid_t uid)
 {
-       int ret = 0;
+       int ret;
        pkgmgrinfo_pkginfo_h handle;
        pkgmgrinfo_appinfo_filter_h filter;
 
@@ -541,13 +508,12 @@ static int _app2sd_kill_running_app(const char *pkgid, uid_t uid)
 
 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 ret;
+       char mmc_path[FILENAME_MAX];
+       char application_path[FILENAME_MAX];
+       char loopback_device[FILENAME_MAX];
+       char *sdpath;
+       FILE *fp;
        int mmc_present = 1;
 
        /* validate the function parameter recieved */
@@ -567,14 +533,13 @@ int app2sd_usr_on_demand_setup_exit(const char *pkgid, uid_t uid)
        snprintf(mmc_path, sizeof(mmc_path), "%s", sdpath);
        free(sdpath);
 
-       encoded_id = _app2sd_get_encoded_name(pkgid, uid);
-       if (encoded_id == NULL)
-               return APP2EXT_ERROR_MEMORY_ALLOC_FAILED;
-       snprintf(loopback_device, sizeof(loopback_device), "%s/%s/%s",
-               mmc_path, EXTIMG_DIR, encoded_id);
-       free(encoded_id);
+       ret = _app2sd_get_loopback_device_path(mmc_path, pkgid, uid,
+                       loopback_device, sizeof(loopback_device));
+       if (ret)
+               return ret;
+
        _app2sd_set_application_path(pkgid, uid, application_path,
-               sizeof(application_path));
+                       sizeof(application_path));
 
        if (mmc_present) {
                fp = fopen(loopback_device, "r+");
@@ -586,7 +551,7 @@ int app2sd_usr_on_demand_setup_exit(const char *pkgid, uid_t uid)
        }
 
        ret = __app2sd_finalize_device_setup(pkgid, loopback_device,
-               application_path, uid);
+                       application_path, uid);
        if (ret) {
                _E("failed to finalize device setup");
                return ret;
@@ -597,14 +562,14 @@ 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;
+       int ret;
+       char mmc_path[FILENAME_MAX];
+       char application_path[FILENAME_MAX];
+       char loopback_device[FILENAME_MAX];
+       char *sdpath;
        char *device_node = NULL;
-       FILE *fp = NULL;
+       FILE *fp;
+       int mount_type;
 
        /* validate the function parameter recieved */
        if (pkgid == NULL) {
@@ -624,14 +589,13 @@ int app2sd_usr_pre_app_uninstall(const char *pkgid, uid_t uid)
        free(sdpath);
        sync();
 
-       encoded_id = _app2sd_get_encoded_name(pkgid, uid);
-       if (encoded_id == NULL)
-               return APP2EXT_ERROR_MEMORY_ALLOC_FAILED;
-       snprintf(loopback_device, sizeof(loopback_device), "%s/%s/%s",
-               mmc_path, EXTIMG_DIR, encoded_id);
-       free(encoded_id);
+       ret = _app2sd_get_loopback_device_path(mmc_path, pkgid, uid,
+                       loopback_device, sizeof(loopback_device));
+       if (ret)
+               return ret;
+
        _app2sd_set_application_path(pkgid, uid, application_path,
-               sizeof(application_path));
+                       sizeof(application_path));
 
        /* check app entry is there in sd card or not. */
        fp = fopen(loopback_device, "r+");
@@ -649,71 +613,55 @@ int app2sd_usr_pre_app_uninstall(const char *pkgid, uid_t uid)
 #else
        device_node = _app2sd_find_associated_device_node(loopback_device);
 #endif
-       if (NULL == device_node) {
+       if (device_node == NULL) {
                /* do loopback setup */
 #ifdef TIZEN_FEATURE_APP2SD_DMCRYPT_ENCRYPTION
                ret = _app2sd_dmcrypt_open_device(pkgid, loopback_device,
-                       false, uid, &device_node);
+                               false, uid, &device_node);
                if (ret) {
                        _E("dmcrypt open device error(%d)", ret);
-                       return APP2EXT_ERROR_OPEN_DMCRYPT_DEVICE;
+                       ret = APP2EXT_ERROR_OPEN_DMCRYPT_DEVICE;
+                       goto END;
                }
 #else
                device_node = _app2sd_do_loopback_encryption_setup(pkgid,
-                       loopback_device, uid);
+                               loopback_device, uid);
                if (device_node == NULL) {
                        _E("loopback encryption setup failed");
                        ret = APP2EXT_ERROR_DO_LOSETUP;
                        goto END;
                }
 #endif
-               /* do mounting */
-               ret = _app2sd_mount_app_content(application_path, pkgid,
-                       device_node, MOUNT_TYPE_RW, NULL,
-                       APP2SD_PRE_UNINSTALL, uid);
-               if (ret) {
-                       _E("mount failed");
-                       if (device_node) {
-                               free(device_node);
-                               device_node = NULL;
-                       }
-                       ret = APP2EXT_ERROR_MOUNT_PATH;
-                       goto END;
-               }
+               mount_type = MOUNT_TYPE_RW;
        } else {
-               /* do re-mounting */
-               ret = _app2sd_mount_app_content(application_path, pkgid,
-                       device_node, MOUNT_TYPE_RW_REMOUNT, NULL,
-                       APP2SD_PRE_UNINSTALL, uid);
-               if (ret) {
-                       _E("remount failed");
-                       if (device_node) {
-                               free(device_node);
-                               device_node = NULL;
-                       }
-                       ret = APP2EXT_ERROR_MOUNT_PATH;
-                       goto END;
-               }
+               mount_type = MOUNT_TYPE_RW_REMOUNT;
        }
-       if (device_node) {
-               free(device_node);
-               device_node = NULL;
+
+       /* do mounting */
+       ret = _app2sd_mount_app_content(application_path, pkgid,
+                       device_node, mount_type, NULL,
+                       APP2SD_PRE_UNINSTALL, uid);
+       if (ret) {
+               _E("mount failed");
+               ret = APP2EXT_ERROR_MOUNT_PATH;
        }
 
 END:
+       if (device_node)
+               free(device_node);
+
        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 app_mmc_path[FILENAME_MAX] = { 0, };
-       char loopback_device[FILENAME_MAX] = { 0, };
-       char *sdpath = NULL;
-       char *encoded_id = NULL;
-       int ret = APP2EXT_SUCCESS;
+       char mmc_path[FILENAME_MAX];
+       char application_path[FILENAME_MAX];
+       char app_mmc_path[FILENAME_MAX];
+       char loopback_device[FILENAME_MAX];
+       char *sdpath;
+       int ret;
 
        /* validate the function parameter recieved */
        if (pkgid == NULL) {
@@ -733,38 +681,34 @@ int app2sd_usr_post_app_uninstall(const char *pkgid, uid_t uid)
        free(sdpath);
        sync();
 
-       encoded_id = _app2sd_get_encoded_name(pkgid, uid);
-       if (encoded_id == NULL)
-               return APP2EXT_ERROR_MEMORY_ALLOC_FAILED;
-       snprintf(loopback_device, sizeof(loopback_device), "%s/%s/%s",
-               mmc_path, EXTIMG_DIR, encoded_id);
-       free(encoded_id);
+       ret = _app2sd_get_loopback_device_path(mmc_path, pkgid, uid,
+                       loopback_device, sizeof(loopback_device));
+       if (ret)
+               goto END;
 
        _app2sd_set_application_path(pkgid, uid, application_path,
-               sizeof(application_path));
+                       sizeof(application_path));
        snprintf(app_mmc_path, sizeof(app_mmc_path), "%s/.mmc",
-               application_path);
+                       application_path);
 
        ret = __app2sd_finalize_device_setup(pkgid, loopback_device,
-               application_path, uid);
+                       application_path, uid);
        if (ret) {
                _E("failed to finalize device setup");
-               return ret;
+               goto END;
        }
 
        /* delete the loopback device from the SD card */
        ret = _app2sd_delete_loopback_device(loopback_device);
        if (ret) {
-               _E("unable to delete the " \
-                       "loopback device from the SD Card");
+               _E("unable to delete the loopback device from the SD Card");
                ret =  APP2EXT_ERROR_DELETE_LOOPBACK_DEVICE;
                goto END;
        }
 
        ret = _app2sd_delete_directory(app_mmc_path);
        if (ret) {
-               _E("unable to delete the directory (%s)",
-               application_path);
+               _E("unable to delete the directory (%s)", application_path);
                goto END;
        }
 
@@ -791,16 +735,17 @@ END:
 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, };
+       int ret;
+       int pkgmgr_ret;
+       char *sdpath;
+       char *image_path;
+       char mmc_path[FILENAME_MAX];
+       INSTALL_LOCATION pkgmgr_move_type;
 
        /* validate function arguments */
-       if (pkgid == NULL || dir_list == NULL
-               || move_type < APP2EXT_MOVE_TO_EXT
-               || move_type > APP2EXT_MOVE_TO_PHONE) {
+       if (pkgid == NULL || dir_list == NULL ||
+                       move_type < APP2EXT_MOVE_TO_EXT ||
+                       move_type > APP2EXT_MOVE_TO_PHONE) {
                _E("invalid function arguments");
                return APP2EXT_ERROR_INVALID_ARGUMENTS;
        }
@@ -821,35 +766,34 @@ int app2sd_usr_pre_move_installed_app(const char *pkgid,
                return ret;
        }
 
-       ret = _app2sd_usr_move_app(pkgid, move_type, dir_list, uid, mmc_path, &image_path);
+       ret = _app2sd_usr_move_app(pkgid, move_type, dir_list, uid, mmc_path,
+                       &image_path);
        if (ret) {
                _D("unable to move application");
                return ret;
        }
 
-       /* if move is completed, then update installed storage to pkgmgr_parser db */
+       /* 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, image_path, uid);
-               if (pkgmgr_ret < 0) {
-                       _E("failed 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;
-               }
+               pkgmgr_move_type = INSTALL_EXTERNAL;
        } else {
-               pkgmgr_ret = pkgmgrinfo_pkginfo_set_usr_installed_storage(pkgid,
-                               INSTALL_INTERNAL, image_path, uid);
-               if (pkgmgr_ret < 0) {
-                       _E("failed to update installed location to db " \
-                               "[%s, %d] of uid(%d), pkgmgr_ret(%d)",
-                               pkgid, INSTALL_INTERNAL, uid, pkgmgr_ret);
-                       return APP2EXT_ERROR_PKGMGR_ERROR;
-               }
+               image_path = NULL;
+               pkgmgr_move_type = INSTALL_INTERNAL;
+       }
+
+       pkgmgr_ret = pkgmgrinfo_pkginfo_set_usr_installed_storage(pkgid,
+                       pkgmgr_move_type, image_path, uid);
+       if (pkgmgr_ret < 0) {
+               _E("failed to update installed location to db [%s, %d] "
+                               "of uid(%d), pkgmgr_ret(%d)",
+                               pkgid, pkgmgr_move_type, uid, pkgmgr_ret);
+               return APP2EXT_ERROR_PKGMGR_ERROR;
        }
 
        if (image_path)
@@ -862,16 +806,15 @@ int app2sd_usr_pre_move_installed_app(const char *pkgid,
 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;
+       int ret;
+       char mmc_path[FILENAME_MAX];
+       char application_path[FILENAME_MAX];
+       char loopback_device[FILENAME_MAX];
+       char *sdpath;
 
        /* validate function arguments */
-       if (pkgid == NULL || move_type < APP2EXT_MOVE_TO_EXT
-               || move_type > APP2EXT_MOVE_TO_PHONE) {
+       if (pkgid == NULL || move_type < APP2EXT_MOVE_TO_EXT ||
+                       move_type > APP2EXT_MOVE_TO_PHONE) {
                _E("invalid function arguments");
                return APP2EXT_ERROR_INVALID_ARGUMENTS;
        }
@@ -889,18 +832,16 @@ int app2sd_usr_post_move_installed_app(const char *pkgid,
        free(sdpath);
        sync();
 
-       encoded_id = _app2sd_get_encoded_name(pkgid, uid);
-       if (encoded_id == NULL)
-               return APP2EXT_ERROR_MEMORY_ALLOC_FAILED;
+       ret = _app2sd_get_loopback_device_path(mmc_path, pkgid, uid,
+                       loopback_device, sizeof(loopback_device));
+       if (ret)
+               return ret;
 
-       snprintf(loopback_device, sizeof(loopback_device), "%s/%s/%s",
-                       mmc_path, EXTIMG_DIR, encoded_id);
-       free(encoded_id);
        _app2sd_set_application_path(pkgid, uid, application_path,
-               sizeof(application_path));
+                       sizeof(application_path));
 
        ret = __app2sd_finalize_device_setup(pkgid, loopback_device,
-               application_path, uid);
+                       application_path, uid);
        if (ret) {
                _E("failed to finalize device setup");
                return ret;
@@ -913,22 +854,20 @@ int app2sd_usr_post_move_installed_app(const char *pkgid,
 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;
-       char *device_node = NULL;
-       char *encoded_id = NULL;
-       char *temp_encoded_id = NULL;
-       int len = 0;
+       int ret;
+       char app2sd_path[FILENAME_MAX];
+       char mmc_path[FILENAME_MAX];
+       char loopback_device[FILENAME_MAX];
+       char application_path[FILENAME_MAX];
+       char *sdpath;
+       char temp_pkgid[FILENAME_MAX];
+       char temp_loopback_device[FILENAME_MAX];
+       char temp_application_path[FILENAME_MAX];
+       char *device_node;
        unsigned long long curr_size = 0;
-       FILE *fp = NULL;
+       FILE *fp;
        int reqd_disk_size = size + ceil(size * 0.2);
+       int mount_type;
 
        /* validate function arguments*/
        if (pkgid == NULL || dir_list == NULL || size <= 0) {
@@ -942,19 +881,18 @@ int app2sd_usr_pre_app_upgrade(const char *pkgid, GList *dir_list,
                _E("MMC not present OR Not ready (%d)", ret);
                return APP2EXT_ERROR_MMC_STATUS;
        }
-       snprintf(app2sd_path, sizeof(app2sd_path), "%s/%s",
-               sdpath, EXTIMG_DIR);
+       snprintf(mmc_path, sizeof(mmc_path), "%s", sdpath);
+       snprintf(app2sd_path, sizeof(app2sd_path), "%s/%s", sdpath, EXTIMG_DIR);
        free(sdpath);
        sync();
 
-       encoded_id = _app2sd_get_encoded_name(pkgid, uid);
-       if (encoded_id == NULL)
-               return APP2EXT_ERROR_MEMORY_ALLOC_FAILED;
-       snprintf(loopback_device, sizeof(loopback_device), "%s/%s",
-               app2sd_path, encoded_id);
-       free(encoded_id);
+       ret = _app2sd_get_loopback_device_path(mmc_path, pkgid, uid,
+                       loopback_device, sizeof(loopback_device));
+       if (ret)
+               return ret;
+
        _app2sd_set_application_path(pkgid, uid, application_path,
-               sizeof(application_path));
+                       sizeof(application_path));
 
        /* check app entry is there in sd card or not. */
        fp = fopen(loopback_device, "r+");
@@ -972,83 +910,19 @@ int app2sd_usr_pre_app_upgrade(const char *pkgid, GList *dir_list,
                return APP2EXT_ERROR_LOOPBACK_DEVICE_UNAVAILABLE;
        }
        if ((int)curr_size < reqd_disk_size) {
-               len = strlen(pkgid) + strlen(".new");
-               temp_pkgid = calloc(len + 1, sizeof(char));
-               if (temp_pkgid == NULL) {
-                       _E("memory alloc failed");
-                       return APP2EXT_ERROR_MEMORY_ALLOC_FAILED;
-               }
-               snprintf(temp_pkgid, len + 1, "%s.new", pkgid);
-
-               if (_is_global(uid)) {
-                       len = strlen(tzplatform_getenv(TZ_SYS_RW_APP)) + strlen(temp_pkgid) + 1;
-                       temp_application_path = calloc(len + 1, sizeof(char));
-                       if (temp_application_path == NULL) {
-                               _E("memory alloc failed");
-                               free(temp_pkgid);
-                               return APP2EXT_ERROR_MEMORY_ALLOC_FAILED;
-                       }
-                       snprintf(temp_application_path, len + 1, "%s/%s",
-                               tzplatform_getenv(TZ_SYS_RW_APP), temp_pkgid);
-
-                       temp_encoded_id = _app2sd_get_encoded_name((const char *)temp_pkgid, uid);
-                       if (temp_encoded_id == NULL) {
-                               free(temp_pkgid);
-                               free(temp_application_path);
-                               return APP2EXT_ERROR_MEMORY_ALLOC_FAILED;
-                       }
-                       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");
-                               free(temp_pkgid);
-                               free(temp_application_path);
-                               free(temp_encoded_id);
-                               return APP2EXT_ERROR_MEMORY_ALLOC_FAILED;
-                       }
-                       snprintf(temp_loopback_device, len + 1, "%s/%s",
-                               app2sd_path, temp_encoded_id);
-                       free(temp_encoded_id);
-               } else {
-                       tzplatform_set_user(uid);
-                       len = strlen(tzplatform_getenv(TZ_USER_APP)) + strlen(temp_pkgid) + 1;
-                       temp_application_path = calloc(len + 1, sizeof(char));
-                       if (temp_application_path == NULL) {
-                               _E("memory alloc failed");
-                               free(temp_pkgid);
-                               return APP2EXT_ERROR_MEMORY_ALLOC_FAILED;
-                       }
-                       snprintf(temp_application_path, len + 1, "%s/%s",
-                               tzplatform_getenv(TZ_USER_APP), temp_pkgid);
-
-                       temp_encoded_id = _app2sd_get_encoded_name((const char *)temp_pkgid, uid);
-                       if (temp_encoded_id == NULL) {
-                               free(temp_pkgid);
-                               free(temp_application_path);
-                               return APP2EXT_ERROR_MEMORY_ALLOC_FAILED;
-                       }
-                       snprintf(temp_uid, 32, "%d", uid);
-                       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");
-                               free(temp_pkgid);
-                               free(temp_application_path);
-                               free(temp_encoded_id);
-                               return APP2EXT_ERROR_MEMORY_ALLOC_FAILED;
-                       }
-                       snprintf(temp_loopback_device, len + 1, "%s/%s",
-                               app2sd_path, temp_encoded_id);
-                       free(temp_encoded_id);
-                       tzplatform_reset_user();
-               }
+               snprintf(temp_pkgid, sizeof(temp_pkgid), "%s.new", pkgid);
+               ret = _app2sd_get_loopback_device_path(mmc_path, temp_pkgid,
+                               uid, temp_loopback_device,
+                               sizeof(temp_loopback_device));
+               if (ret)
+                       return ret;
+               _app2sd_set_application_path(pkgid, uid, temp_application_path,
+                               sizeof(temp_application_path));
+
                ret = _app2sd_update_loopback_device_size(pkgid,
-                       loopback_device, application_path, temp_pkgid,
-                       temp_loopback_device, temp_application_path,
-                       reqd_disk_size, dir_list, uid);
-               free(temp_pkgid);
-               free(temp_application_path);
-               free(temp_loopback_device);
+                               loopback_device, application_path, temp_pkgid,
+                               temp_loopback_device, temp_application_path,
+                               reqd_disk_size, dir_list, uid);
                if (APP2EXT_SUCCESS != ret) {
                        _E("failed to update loopback device size");
                        return ret;
@@ -1062,56 +936,39 @@ int app2sd_usr_pre_app_upgrade(const char *pkgid, GList *dir_list,
 #else
        device_node = _app2sd_find_associated_device_node(loopback_device);
 #endif
-       if (NULL == device_node) {
+       if (device_node == NULL) {
                /* do loopback setup */
 #ifdef TIZEN_FEATURE_APP2SD_DMCRYPT_ENCRYPTION
                ret = _app2sd_dmcrypt_open_device(pkgid, loopback_device,
-                       false, uid, &device_node);
+                               false, uid, &device_node);
                if (ret) {
                        _E("dmcrypt open device error");
                        return APP2EXT_ERROR_OPEN_DMCRYPT_DEVICE;
                }
 #else
                device_node = _app2sd_do_loopback_encryption_setup(pkgid,
-                       loopback_device, uid);
+                               loopback_device, uid);
                if (device_node == NULL) {
                        _E("loopback encryption setup failed");
                        return APP2EXT_ERROR_DO_LOSETUP;
                }
 #endif
-
-               /* do mounting */
-               ret = _app2sd_mount_app_content(application_path, pkgid,
-                       device_node, MOUNT_TYPE_RW, dir_list,
-                       APP2SD_PRE_UPGRADE, uid);
-               if (ret) {
-                       _E("mount failed");
-                       if (device_node) {
-                               free(device_node);
-                               device_node = NULL;
-                       }
-                       return APP2EXT_ERROR_MOUNT_PATH;
-               }
+               mount_type = MOUNT_TYPE_RW;
        } else {
-               /* do re-mounting */
-               ret = _app2sd_mount_app_content(application_path, pkgid,
-                       device_node, MOUNT_TYPE_RW_REMOUNT, NULL,
-                       APP2SD_PRE_UPGRADE, uid);
-               if (ret) {
-                       _E("remount failed");
-                       if (device_node) {
-                               free(device_node);
-                               device_node = NULL;
-                       }
-                       return APP2EXT_ERROR_MOUNT_PATH;
-               }
+               mount_type = MOUNT_TYPE_RW_REMOUNT;
        }
 
-       if (device_node) {
+       /* do mounting */
+       ret = _app2sd_mount_app_content(application_path, pkgid, device_node,
+                       mount_type, dir_list, APP2SD_PRE_UPGRADE, uid);
+       if (ret) {
+               _E("mount failed");
                free(device_node);
-               device_node = NULL;
+               return APP2EXT_ERROR_MOUNT_PATH;
        }
 
+       free(device_node);
+
        sync();
        return ret;
 }
@@ -1119,17 +976,16 @@ int app2sd_usr_pre_app_upgrade(const char *pkgid, GList *dir_list,
 int app2sd_usr_post_app_upgrade(const char *pkgid,
                app2ext_status install_status, 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;
-       int pkgmgr_ret = 0;
+       char mmc_path[FILENAME_MAX];
+       char loopback_device[FILENAME_MAX];
+       char application_path[FILENAME_MAX];
+       char *sdpath;
+       int ret;
+       int pkgmgr_ret;
 
        /* validate the function parameter recieved */
-       if (pkgid == NULL || install_status < APP2EXT_STATUS_FAILED
-               || install_status > APP2EXT_STATUS_SUCCESS) {
+       if (pkgid == NULL || install_status < APP2EXT_STATUS_FAILED ||
+                       install_status > APP2EXT_STATUS_SUCCESS) {
                _E("invalid func parameters");
                return APP2EXT_ERROR_INVALID_ARGUMENTS;
        }
@@ -1144,28 +1000,27 @@ int app2sd_usr_post_app_upgrade(const char *pkgid,
        free(sdpath);
        sync();
 
-       encoded_id = _app2sd_get_encoded_name(pkgid, uid);
-       if (encoded_id == NULL)
-               return APP2EXT_ERROR_MEMORY_ALLOC_FAILED;
-       snprintf(loopback_device, sizeof(loopback_device), "%s/%s/%s",
-               mmc_path, EXTIMG_DIR, encoded_id);
-       free(encoded_id);
+       ret = _app2sd_get_loopback_device_path(mmc_path, pkgid, uid,
+                       loopback_device, sizeof(loopback_device));
+       if (ret)
+               return ret;
+
        _app2sd_set_application_path(pkgid, uid, application_path,
-               sizeof(application_path));
+                       sizeof(application_path));
 
        ret = __app2sd_finalize_device_setup(pkgid, loopback_device,
-               application_path, uid);
+                       application_path, uid);
        if (ret) {
                _E("failed to finalize device setup");
                return ret;
        }
 
        pkgmgr_ret = pkgmgrinfo_pkginfo_set_usr_installed_storage(pkgid,
-               INSTALL_EXTERNAL, loopback_device, 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)",
-                       pkgid, INSTALL_EXTERNAL, uid, pkgmgr_ret);
+               _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;
        }
 
@@ -1175,12 +1030,11 @@ int app2sd_usr_post_app_upgrade(const char *pkgid,
 
 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;
+       char mmc_path[FILENAME_MAX];
+       char loopback_device[FILENAME_MAX];
+       char application_path[FILENAME_MAX];
+       char *sdpath;
+       int ret;
 
        /* validate the function parameter recieved */
        if (pkgid == NULL) {
@@ -1198,16 +1052,16 @@ int app2sd_usr_force_clean(const char *pkgid, uid_t uid)
        free(sdpath);
        sync();
 
-       encoded_id = _app2sd_get_encoded_name(pkgid, uid);
-       if (encoded_id == NULL)
-               return APP2EXT_ERROR_MEMORY_ALLOC_FAILED;
-       snprintf(loopback_device, sizeof(loopback_device), "%s/%s/%s",
-               mmc_path, EXTIMG_DIR, encoded_id);
-       free(encoded_id);
+       ret = _app2sd_get_loopback_device_path(mmc_path, pkgid, uid,
+                       loopback_device, sizeof(loopback_device));
+       if (ret)
+               return ret;
+
        _app2sd_set_application_path(pkgid, uid, application_path,
-               sizeof(application_path));
+                       sizeof(application_path));
 
-       ret = _app2sd_force_clean(pkgid, application_path, loopback_device, uid);
+       ret = _app2sd_force_clean(pkgid, application_path, loopback_device,
+                       uid);
 
        sync();
        return ret;
@@ -1215,15 +1069,15 @@ int app2sd_usr_force_clean(const char *pkgid, uid_t uid)
 
 int app2sd_enable_full_pkg(void)
 {
-       int ret = APP2EXT_SUCCESS;
-       char buf[FILENAME_MAX] = { 0, };
-       char app2sd_path[FILENAME_MAX] = { 0, };
-       char loopback_device[FILENAME_MAX] = { 0, };
-       char *sdpath = NULL;
+       int ret;
+       char buf[FILENAME_MAX];
+       char app2sd_path[FILENAME_MAX];
+       char loopback_device[FILENAME_MAX];
+       char *sdpath;
        char *pkgid = NULL;
-       DIR *dir = NULL;
-       struct dirent *entry = NULL;
-       uid_t uid = 0;
+       DIR *dir;
+       struct dirent *entry;
+       uid_t uid;
 
        /* check whether MMC is present or not */
        ret = _app2sd_check_mmc_status(&sdpath);
@@ -1231,8 +1085,7 @@ int app2sd_enable_full_pkg(void)
                _E("MMC not present OR Not ready (%d)", ret);
                return APP2EXT_ERROR_MMC_STATUS;
        }
-       snprintf(app2sd_path, sizeof(app2sd_path), "%s/%s",
-               sdpath, EXTIMG_DIR);
+       snprintf(app2sd_path, sizeof(app2sd_path), "%s/%s", sdpath, EXTIMG_DIR);
        free(sdpath);
 
        dir = opendir(app2sd_path);
@@ -1251,12 +1104,12 @@ int app2sd_enable_full_pkg(void)
 
        while ((entry = readdir(dir)) != NULL) {
                if (strcmp(entry->d_name, ".") == 0 ||
-                       strcmp(entry->d_name, "..") == 0)
+                               strcmp(entry->d_name, "..") == 0)
                        continue;
                snprintf(loopback_device, sizeof(loopback_device), "%s/%s",
-                       app2sd_path, entry->d_name);
+                               app2sd_path, entry->d_name);
                ret = _app2sd_get_info_from_db(loopback_device,
-                       &pkgid, &uid);
+                               &pkgid, &uid);
                if (ret) {
                        _W("failed to get info from db, continue");
                        continue;
@@ -1273,10 +1126,8 @@ int app2sd_enable_full_pkg(void)
                }
        }
 
-       if (pkgid) {
+       if (pkgid)
                free(pkgid);
-               pkgid = NULL;
-       }
        closedir(dir);
 
        sync();
@@ -1307,7 +1158,8 @@ int app2sd_disable_full_pkg(void)
                return APP2EXT_ERROR_SQLITE_REGISTRY;
        }
 
-       ret = _app2sd_get_foreach_info_from_db((app2sd_info_cb)_app2sd_info_cb_func);
+       ret = _app2sd_get_foreach_info_from_db(
+                       (app2sd_info_cb)_app2sd_info_cb_func);
        if (ret)
                _E("disable full pkg error(%d)", ret);
 
@@ -1318,34 +1170,25 @@ int app2sd_disable_full_pkg(void)
 static int _app2sd_migrate_legacy_image(const char *pkgid, const char *passwd,
                const char *mmc_path, uid_t uid)
 {
-       int ret = 0;
-       char *device_node = NULL;
-       char *encoded_id = NULL;
-       char new_image[FILENAME_MAX] = { 0, };
-       char legacy_image[FILENAME_MAX] = { 0, };
-       char application_path[FILENAME_MAX] = { 0, };
+       int ret;
+       char *device_node;
+       char *encoded_id;
+       char new_image[FILENAME_MAX];
+       char legacy_image[FILENAME_MAX];
+       char application_path[FILENAME_MAX];
        uid_t default_uid = tzplatform_getuid(TZ_SYS_DEFAULT_USER);
 
-       if (_is_global(uid)) {
-               snprintf(application_path, sizeof(application_path), "%s/%s",
-                       tzplatform_getenv(TZ_SYS_RW_APP), pkgid);
-       } else {
-               tzplatform_set_user(uid);
-               snprintf(application_path, sizeof(application_path), "%s/%s",
-                       tzplatform_getenv(TZ_USER_APP), pkgid);
-               tzplatform_reset_user();
-       }
+       _app2sd_set_application_path(pkgid, uid, application_path,
+                       sizeof(application_path));
 
        /* make the information and insert to DB */
        encoded_id = _app2sd_get_encoded_name(pkgid, uid);
        if (encoded_id == NULL)
                return APP2EXT_ERROR_MEMORY_ALLOC_FAILED;
-       snprintf(new_image, sizeof(new_image), "%s/%s",
-               mmc_path, encoded_id);
+       snprintf(new_image, sizeof(new_image), "%s/%s", mmc_path, encoded_id);
        free(encoded_id);
 
-       snprintf(legacy_image, sizeof(legacy_image), "%s/%s",
-               mmc_path, pkgid);
+       snprintf(legacy_image, sizeof(legacy_image), "%s/%s", mmc_path, pkgid);
 
        ret = _app2sd_rename_dir(legacy_image, new_image);
        if (ret) {
@@ -1354,11 +1197,11 @@ static int _app2sd_migrate_legacy_image(const char *pkgid, const char *passwd,
        }
 
        ret = pkgmgrinfo_pkginfo_set_usr_installed_storage(pkgid,
-               INSTALL_EXTERNAL, new_image, uid);
+                       INSTALL_EXTERNAL, new_image, uid);
        if (ret < 0) {
-               _E("fail to update installed location " \
-                       "to db[%s, %d] of uid(%d), ret(%d)",
-                       pkgid, INSTALL_EXTERNAL, uid, ret);
+               _E("fail to update installed location "
+                               "to db[%s, %d] of uid(%d), ret(%d)",
+                               pkgid, INSTALL_EXTERNAL, uid, ret);
                return APP2EXT_ERROR_PKGMGR_ERROR;
        }
 
@@ -1386,7 +1229,7 @@ static int _app2sd_migrate_legacy_image(const char *pkgid, const char *passwd,
        }
 
        ret = _app2sd_dmcrypt_open_device(pkgid, new_image, false,
-               uid, &device_node);
+                       uid, &device_node);
        if (ret) {
                _E("dmcrypt open device error(%d)", ret);
                return APP2EXT_ERROR_OPEN_DMCRYPT_DEVICE;
@@ -1403,7 +1246,7 @@ static int _app2sd_migrate_legacy_image(const char *pkgid, const char *passwd,
 
        /* do loopback setup */
        device_node = _app2sd_do_loopback_encryption_setup(pkgid,
-               new_image, uid);
+                       new_image, uid);
        if (device_node == NULL) {
                _E("loopback encryption setup failed");
                return APP2EXT_ERROR_DO_LOSETUP;
@@ -1412,21 +1255,15 @@ static int _app2sd_migrate_legacy_image(const char *pkgid, const char *passwd,
 
        /* do mounting */
        ret = _app2sd_mount_app_content(application_path, pkgid,
-               device_node, MOUNT_TYPE_RW, NULL,
-               APP2SD_MIGRATE_LEGACY, uid);
+                       device_node, MOUNT_TYPE_RW, NULL,
+                       APP2SD_MIGRATE_LEGACY, uid);
        if (ret) {
                _E("mount failed");
-               if (device_node) {
-                       free(device_node);
-                       device_node = NULL;
-               }
+               free(device_node);
                return APP2EXT_ERROR_MOUNT_PATH;
        }
 
-       if (device_node) {
-               free(device_node);
-               device_node = NULL;
-       }
+       free(device_node);
 
        sync();
        return APP2EXT_SUCCESS;
@@ -1434,11 +1271,11 @@ static int _app2sd_migrate_legacy_image(const char *pkgid, const char *passwd,
 
 int app2sd_pre_migrate_legacy(const char *pkgid, uid_t uid)
 {
-       int ret = APP2EXT_SUCCESS;
-       char *passwd = NULL;
-       char *sdpath = NULL;
-       char mmc_path[FILENAME_MAX] = { 0, };
-       char file_path[FILENAME_MAX] = { 0, };
+       int ret;
+       char *passwd;
+       char *sdpath;
+       char mmc_path[FILENAME_MAX];
+       char file_path[FILENAME_MAX];
        uid_t default_uid = tzplatform_getuid(TZ_SYS_DEFAULT_USER);
        char *filename;
 
@@ -1461,15 +1298,15 @@ int app2sd_pre_migrate_legacy(const char *pkgid, uid_t uid)
        /* In the upgrade script, the information
         * of legacy image are filled with default user-id,
         * pkgid(from old db), passwd(from old db) and
-        * empty filename in app2sd db. */
+        * empty filename in app2sd db.
+        */
        filename = _app2sd_get_filename_from_db(pkgid, default_uid);
        if (filename == NULL) {
                passwd = _app2sd_get_password_from_db(pkgid, default_uid);
                if (passwd) {
                        ret = _app2sd_migrate_legacy_image(pkgid, passwd,
-                               mmc_path, uid);
+                                       mmc_path, uid);
                        free(passwd);
-                       passwd = NULL;
                } else {
                        _E("invalid package info");
                        return APP2EXT_ERROR_INVALID_PACKAGE;
@@ -1488,12 +1325,11 @@ int app2sd_pre_migrate_legacy(const char *pkgid, uid_t uid)
 
 int app2sd_post_migrate_legacy(const char *pkgid, uid_t uid)
 {
-       int ret = APP2EXT_SUCCESS;
-       char *sdpath = NULL;
-       char *encoded_id = NULL;
-       char mmc_path[FILENAME_MAX] = { 0, };
-       char application_path[FILENAME_MAX] = { 0, };
-       char loopback_device[FILENAME_MAX] = { 0, };
+       int ret;
+       char *sdpath;
+       char mmc_path[FILENAME_MAX];
+       char application_path[FILENAME_MAX];
+       char loopback_device[FILENAME_MAX];
 
        /* check whether MMC is present or not */
        ret = _app2sd_check_mmc_status(&sdpath);
@@ -1501,30 +1337,19 @@ int app2sd_post_migrate_legacy(const char *pkgid, uid_t uid)
                _E("MMC not present OR Not ready (%d)", ret);
                return APP2EXT_ERROR_MMC_STATUS;
        }
-       snprintf(mmc_path, sizeof(mmc_path), "%s/%s",
-               sdpath, EXTIMG_DIR);
+       snprintf(mmc_path, sizeof(mmc_path), "%s/%s", sdpath, EXTIMG_DIR);
        free(sdpath);
 
-       if (_is_global(uid)) {
-               snprintf(application_path, sizeof(application_path), "%s/%s",
-                       tzplatform_getenv(TZ_SYS_RW_APP), pkgid);
-       } else {
-               tzplatform_set_user(uid);
-               snprintf(application_path, sizeof(application_path), "%s/%s",
-                       tzplatform_getenv(TZ_USER_APP), pkgid);
-               tzplatform_reset_user();
-       }
-
-       encoded_id = _app2sd_get_encoded_name(pkgid, uid);
-       if (encoded_id == NULL)
-               return APP2EXT_ERROR_MEMORY_ALLOC_FAILED;
+       ret = _app2sd_get_loopback_device_path(mmc_path, pkgid, uid,
+                       loopback_device, sizeof(loopback_device));
 
-       snprintf(loopback_device, sizeof(loopback_device), "%s/%s",
-               mmc_path, encoded_id);
-       free(encoded_id);
+       _app2sd_set_application_path(pkgid, uid, application_path,
+                       sizeof(application_path));
+       if (ret)
+               return ret;
 
        ret = __app2sd_finalize_device_setup(pkgid, loopback_device,
-               application_path, uid);
+                       application_path, uid);
        if (ret) {
                _E("failed to finalize device setup");
                return ret;
@@ -1536,13 +1361,13 @@ int app2sd_post_migrate_legacy(const char *pkgid, uid_t uid)
 /* this function is called when sdcard is inserted */
 int app2sd_migrate_legacy_all(void)
 {
-       int ret = APP2EXT_SUCCESS;
-       char buf[FILENAME_MAX] = { 0, };
-       char app2sd_path[FILENAME_MAX] = { 0, };
-       char loopback_device[FILENAME_MAX] = { 0, };
-       char *sdpath = NULL;
-       DIR *dir = NULL;
-       struct dirent *entry = NULL;
+       int ret;
+       char buf[FILENAME_MAX];
+       char app2sd_path[FILENAME_MAX];
+       char loopback_device[FILENAME_MAX];
+       char *sdpath;
+       DIR *dir;
+       struct dirent *entry;
        uid_t default_uid = tzplatform_getuid(TZ_SYS_DEFAULT_USER);
        pkgmgr_client *pc;
 
@@ -1552,8 +1377,7 @@ int app2sd_migrate_legacy_all(void)
                _E("MMC not present OR Not ready (%d)", ret);
                return APP2EXT_ERROR_MMC_STATUS;
        }
-       snprintf(app2sd_path, sizeof(app2sd_path), "%s/%s",
-               sdpath, EXTIMG_DIR);
+       snprintf(app2sd_path, sizeof(app2sd_path), "%s/%s", sdpath, EXTIMG_DIR);
        free(sdpath);
 
        dir = opendir(app2sd_path);
@@ -1572,16 +1396,17 @@ int app2sd_migrate_legacy_all(void)
 
        while ((entry = readdir(dir)) != NULL) {
                if (strcmp(entry->d_name, ".") == 0 ||
-                       strcmp(entry->d_name, "..") == 0)
+                               strcmp(entry->d_name, "..") == 0)
                        continue;
                snprintf(loopback_device, sizeof(loopback_device), "%s/%s",
-                       app2sd_path, entry->d_name);
+                               app2sd_path, entry->d_name);
                /* check losetup image */
                if (_app2sd_check_is_luks_device(loopback_device) == 0) {
                        /* call installer backend
-                          to change access-rule and broadcast this update */
+                        * to change access-rule and broadcast this update
+                        */
                        ret = pkgmgr_client_usr_migrate_external_image(pc,
-                               entry->d_name, default_uid);
+                                       entry->d_name, default_uid);
                        if (ret < 0)
                                _E("failed to request migration, ret(%d)", ret);
                }
@@ -1592,4 +1417,3 @@ int app2sd_migrate_legacy_all(void)
 
        return ret;
 }
-
index 12d213d..894ca47 100644 (file)
 
 static int _app2sd_make_directory(const char *path, uid_t uid)
 {
-       int ret = 0;
-       int fd = -1;
+       int ret;
+       int fd;
        mode_t mode = DIR_PERMS;
        struct passwd pwd;
        struct passwd *pwd_result;
-       char buf[1024] = { 0, };
+       char buf[1024];
 
        ret = _app2sd_delete_directory(path);
        if (ret) {
@@ -59,8 +59,7 @@ static int _app2sd_make_directory(const char *path, uid_t uid)
        ret = mkdir(path, mode);
        if (ret) {
                if (errno != EEXIST) {
-                       _E("create directory failed," \
-                               " error no is (%d)", errno);
+                       _E("create directory failed, error is (%d)", errno);
                        return APP2EXT_ERROR_CREATE_DIRECTORY;
                }
        }
@@ -92,11 +91,12 @@ static int _app2sd_make_directory(const char *path, uid_t uid)
 
 char *_app2sd_find_associated_device_node(const char *loopback_device)
 {
-       char *ret_result = NULL;
+       char *ret_result;
        char delims[] = ":";
-       char *result = NULL;
-       char dev[FILENAME_MAX] = { 0, };
+       char *result;
+       char dev[FILENAME_MAX];
        char *devnode = NULL;
+       char *saveptr;
 
        result = (char *)_app2sd_find_associated_device(loopback_device);
        if (result == NULL) {
@@ -112,7 +112,6 @@ char *_app2sd_find_associated_device_node(const char *loopback_device)
                free(result);
                return NULL;
        } else {
-               char *saveptr = NULL;
                ret_result = strtok_r(dev, delims, &saveptr);
                if (ret_result)
                        devnode = strdup(ret_result);
@@ -123,78 +122,60 @@ char *_app2sd_find_associated_device_node(const char *loopback_device)
        return devnode;
 }
 
+#define MAX_LOOP_COUNT 256
 char *_app2sd_create_loopdevice_node(void)
 {
-       char *ret_result = NULL;
        mode_t mode = DIR_PERMS;
-       int count = 0;
-       int ret = APP2EXT_SUCCESS;
-       char *result = NULL;
-       FILE *fp = NULL;
+       int count;
+       int ret;
+       char *result;
+       FILE *fp;
+       char dev_path[BUF_SIZE];
+       dev_t dev_node;
 
        result = (char *)_app2sd_find_free_device();
        _D("find_free_device(%s)", result);
 
-       /* validate the result */
-       if (result == NULL || strstr(result, "/dev") == NULL) {
-               _D("no device found, creating device node");
+       if (result && strncmp(result, "/dev", strlen("/dev")) == 0) {
+               _D("device node candidate is (%s)", result);
+               return result;
+       }
 
-               if (result) {
-                       free(result);
-                       result = NULL;
-               }
-               count = 0;
-               char dev_path[BUF_SIZE] = { 0, };
+       _D("no device found, creating device node");
+       if (result)
+               free(result);
+       for (count = 0; count < MAX_LOOP_COUNT; count++) {
                snprintf(dev_path, sizeof(dev_path), "/dev/loop%d", count);
-               while ((fp = fopen(dev_path, "r+")) != NULL) {
-                       count = count + 1;
-                       snprintf(dev_path, sizeof(dev_path), "/dev/loop%d", count);
-                       _D("next dev path for checking is (%s)",
-                                    dev_path);
-                       fclose(fp);
-               }
-               _D("device node candidate is (%s)", dev_path);
-               dev_t dev_node;
-               dev_node = makedev(DEV_MAJOR, count);
-               ret = mknod(dev_path, S_IFBLK | mode, dev_node);
-               if (ret < 0) {
-                       _E("error while creating the device node: errno is (%d)",
-                            errno);
-                       return NULL;
-               }
-               ret_result = (char *)malloc(strlen(dev_path) + 1);
-               if (ret_result == NULL) {
-                       _E("unable to allocate memory");
-                       return NULL;
-               }
-               memset(ret_result, '\0', strlen(dev_path) + 1);
-               memcpy(ret_result, dev_path, strlen(dev_path));
-       } else {
-               ret_result = (char *)malloc(strlen(result) + 1);
-               if (ret_result == NULL) {
-                       _E("malloc failed");
-                       free(result);
-                       result = NULL;
-                       return NULL;
-               }
-               memset(ret_result, '\0', strlen(result) + 1);
-               if (strlen(result) > 0)
-                       memcpy(ret_result, result, strlen(result) - 1);
+               _D("next dev path for checking is (%s)", dev_path);
+               fp = fopen(dev_path, "r+");
+               if (fp == NULL)
+                       break;
+               fclose(fp);
+       }
 
-               free(result);
-               result = NULL;
+       _D("device node candidate is (%s)", dev_path);
 
+       dev_node = makedev(DEV_MAJOR, count);
+       ret = mknod(dev_path, S_IFBLK | mode, dev_node);
+       if (ret < 0) {
+               _E("error while creating the device node: errno (%d)", errno);
+               return NULL;
        }
-       return ret_result;
+
+       result = strdup(dev_path);
+       if (result == NULL)
+               _E("unable to allocate memory");
+
+       return result;
 }
 
 char *_app2sd_do_loopback_encryption_setup(const char *pkgid,
-       const char *loopback_device, uid_t uid)
+               const char *loopback_device, uid_t uid)
 {
-       int ret = APP2EXT_SUCCESS;
-       char *passwd = NULL;
-       char *result = NULL;
-       char *device_node = NULL;
+       int ret;
+       char *passwd;
+       char *result;
+       char *device_node;
 
        if (pkgid == NULL) {
                _E("invalid argument");
@@ -208,27 +189,27 @@ char *_app2sd_do_loopback_encryption_setup(const char *pkgid,
                return NULL;
        }
 
-       if ((passwd = _app2sd_get_password_from_db(pkgid, uid)) == NULL) {
+       passwd = _app2sd_get_password_from_db(pkgid, uid);
+       if (passwd == NULL) {
                passwd = (char *)_app2sd_generate_password();
-               if (NULL == passwd) {
+               if (passwd == NULL) {
                        _E("unable to generate password");
                        return NULL;
-               } else {
-                       if ((ret = _app2sd_set_info_in_db(pkgid,
-                               passwd, loopback_device, uid)) < 0) {
-                               _E("unable to save info");
-                               free(passwd);
-                               passwd = NULL;
-                               return NULL;
-                       }
+               }
+
+               ret = _app2sd_set_info_in_db(pkgid, passwd, loopback_device,
+                               uid);
+               if (ret < 0) {
+                       _E("unable to save info");
+                       free(passwd);
+                       return NULL;
                }
        }
 
        /* get free device node*/
        device_node = _app2sd_create_loopdevice_node();
-       if (NULL == device_node) {
+       if (device_node == NULL) {
                free(passwd);
-               passwd = NULL;
                _E("unable to find free loopback node");
                return NULL;
        }
@@ -236,70 +217,63 @@ char *_app2sd_do_loopback_encryption_setup(const char *pkgid,
        _D("device_node (%s)", device_node);
 
        result = (char *)_app2sd_encrypt_device(device_node,
-               loopback_device, passwd);
+                       loopback_device, passwd);
+       free(passwd);
        if (result == NULL) {
                _E("encryption failed");
-               free(passwd);
-               passwd = NULL;
                return NULL;
-       } else {
-               _D("result (%s)", result);
-               free(result);
-               result = NULL;
-               free(passwd);
-               passwd = NULL;
-               return device_node;
        }
+
+       _D("result (%s)", result);
+       free(result);
+
+       return device_node;
 }
 
 char *_app2sd_do_loopback_duplicate_encryption_setup(const char *pkgid,
                const char *temp_pkgid, const char *temp_loopback_device,
                char *passwd, uid_t uid)
 {
-       char *result = NULL;
-       char *device_node = NULL;
+       char *result;
+       char *device_node;
 
        if (pkgid == NULL || temp_pkgid == NULL ||
-               temp_loopback_device == NULL || passwd == NULL) {
+                       temp_loopback_device == NULL || passwd == NULL) {
                _E("invalid argument");
                return NULL;
        }
 
        /* get free device node*/
        device_node = _app2sd_create_loopdevice_node();
-       if (NULL == device_node) {
+       if (device_node == NULL) {
                _E("unable to find free loopback node");
                return NULL;
        }
        result = (char *)_app2sd_encrypt_device(device_node,
-               temp_loopback_device, passwd);
-       if (result == NULL) {
+                       temp_loopback_device, passwd);
+       /* losetup does not forked or errmsg from losetup */
+       if (result == NULL || strlen(result)) {
                _E("encryption failed");
-               return NULL;
-       } else {
-               if (strlen(result) == 0) {
-                       free(result);
-                       result = NULL;
-                       return device_node;
-               } else {
+               if (result && strlen(result))
                        _E("error is (%s)", result);
+               if (result)
                        free(result);
-                       result = NULL;
-                       return NULL;
-               }
+               return NULL;
        }
 
+       free(result);
+
        return device_node;
 }
 
 int _app2sd_remove_loopback_encryption_setup(const char *loopback_device)
 {
        int ret = APP2EXT_SUCCESS;
-       char *result = NULL;
-       char *dev_node = NULL;
+       char *result;
+       char *dev_node;
 
-       if ((dev_node = _app2sd_find_associated_device_node(loopback_device))
-               == NULL) {
+       dev_node = _app2sd_find_associated_device_node(loopback_device);
+       if (dev_node == NULL) {
                _E("Unable to find the association");
                ret = APP2EXT_ERROR_FIND_ASSOCIATED_DEVICE_NODE;
        }
@@ -313,23 +287,21 @@ int _app2sd_remove_loopback_encryption_setup(const char *loopback_device)
                result = NULL;
        }
 
-       if (dev_node) {
+       if (dev_node)
                free(dev_node);
-               dev_node = NULL;
-       }
 
        return ret;
 }
 
 int _app2sd_remove_all_loopback_encryption_setups(const char *loopback_device)
 {
-       int ret = APP2EXT_SUCCESS;
-       char *result = NULL;
-       char *dev_node = NULL;
+       int ret;
+       char *result;
+       char *dev_node;
        while (1) {
-               if ((dev_node =
-                       _app2sd_find_associated_device_node(loopback_device))
-                       == NULL) {
+               dev_node = _app2sd_find_associated_device_node(
+                               loopback_device);
+               if (dev_node == NULL) {
                        _E("finish to find the association");
                        ret = APP2EXT_SUCCESS;
                        break;
@@ -338,18 +310,13 @@ int _app2sd_remove_all_loopback_encryption_setups(const char *loopback_device)
                _D("find node (%s)", dev_node);
 
                result = (char *)_app2sd_detach_loop_device(dev_node);
+               free(dev_node);
                if (result == NULL) {
                        _E("error in detaching");
                        ret = APP2EXT_ERROR_DETACH_LOOPBACK_DEVICE;
                        break;
-               } else {
-                       free(result);
-                       result = NULL;
-               }
-               if (dev_node) {
-                       free(dev_node);
-                       dev_node = NULL;
                }
+               free(result);
        }
 
        return ret;
@@ -359,10 +326,10 @@ int _app2sd_remove_all_loopback_encryption_setups(const char *loopback_device)
 int _app2sd_dmcrypt_setup_device(const char *pkgid,
                const char *loopback_device, bool is_dup, uid_t uid)
 {
-       int ret = APP2EXT_SUCCESS;
-       char *passwd = NULL;
-       char dmcrypt_setup_cmd[BUF_SIZE] = { 0, };
-       char err_buf[BUF_SIZE] = { 0, };
+       int ret;
+       char *passwd;
+       char dmcrypt_setup_cmd[BUF_SIZE];
+       char err_buf[BUF_SIZE];
 
        if (pkgid == NULL || loopback_device == NULL) {
                _E("invalid argument");
@@ -382,55 +349,46 @@ int _app2sd_dmcrypt_setup_device(const char *pkgid,
                        _E("no password found for (%s)", pkgid);
                        return APP2EXT_ERROR_SQLITE_REGISTRY;
                }
-               passwd = (char *)_app2sd_generate_password();
-               if (NULL == passwd) {
-                       _E("unable to generate password\n");
+               passwd = _app2sd_generate_password();
+               if (passwd == NULL) {
+                       _E("unable to generate password");
                        return APP2EXT_ERROR_PASSWD_GENERATION;
-               } else {
-                       if ((ret = _app2sd_set_info_in_db(pkgid, passwd,
-                               loopback_device, uid)) < 0) {
-                               _E("unable to save password");
-                               free(passwd);
-                               passwd = NULL;
-                               return APP2EXT_ERROR_SQLITE_REGISTRY;
-                       }
+               }
+
+               ret = _app2sd_set_info_in_db(pkgid, passwd, loopback_device,
+                               uid);
+               if (ret < 0) {
+                       _E("unable to save password");
+                       free(passwd);
+                       return APP2EXT_ERROR_SQLITE_REGISTRY;
                }
        }
 
        snprintf(dmcrypt_setup_cmd, sizeof(dmcrypt_setup_cmd),
-               "/bin/echo '%s' | /sbin/cryptsetup -q -i %d " \
-               "-c aes-cbc-lmk -s %d --align-payload=8 luksFormat %s",
-               passwd, DMCRYPT_ITER_TIME, DMCRYPT_KEY_LEN, loopback_device);
-
+                       "/bin/echo '%s' | /sbin/cryptsetup -q -i %d "
+                       "-c aes-cbc-lmk -s %d --align-payload=8 luksFormat %s",
+                       passwd, DMCRYPT_ITER_TIME, DMCRYPT_KEY_LEN,
+                       loopback_device);
+       free(passwd);
        ret = system(dmcrypt_setup_cmd);
        if (ret) {
                strerror_r(errno, err_buf, sizeof(err_buf));
-               _E("Error setting up dmcrypt on app2sd file, " \
-                       "error(%s), ret(%d)", err_buf, ret);
-               if (passwd) {
-                       free(passwd);
-                       passwd = NULL;
-               }
+               _E("Error setting up dmcrypt on app2sd file, error:%s, ret:%d",
+                               err_buf, ret);
                return APP2EXT_ERROR_SETUP_DMCRYPT_DEVICE;
        }
 
-       if (passwd) {
-               free(passwd);
-               passwd = NULL;
-       }
-
        return ret;
 }
 
 int _app2sd_dmcrypt_open_device(const char *pkgid, const char *loopback_device,
                bool is_dup, uid_t uid, char **dev_node)
 {
-       int ret = APP2EXT_SUCCESS;
+       int ret;
        char *passwd;
-       char dmcrypt_open_cmd[BUF_SIZE] = { 0, };
-       char dev_name[BUF_SIZE] = { 0, };
-       char buf[BUF_SIZE] = { 0, };
-       int len = 0;
+       char dmcrypt_open_cmd[BUF_SIZE];
+       char dev_name[BUF_SIZE];
+       char buf[BUF_SIZE];
 
        if (pkgid == NULL || loopback_device == NULL) {
                _E("invalid argument");
@@ -458,13 +416,14 @@ int _app2sd_dmcrypt_open_device(const char *pkgid, const char *loopback_device,
        if (_app2sd_check_is_luks_device(loopback_device) == 0) {
                _W("legacy image format!");
                snprintf(dmcrypt_open_cmd, sizeof(dmcrypt_open_cmd),
-                       "/bin/echo '%s' | /sbin/cryptsetup " \
-                       "-M plain -c aes-cbc-plain -h plain open %s %s",
-                       passwd, loopback_device, dev_name);
+                               "/bin/echo '%s' | /sbin/cryptsetup "
+                               "-M plain -c aes-cbc-plain -h plain open %s %s",
+                               passwd, loopback_device, dev_name);
        } else {
                snprintf(dmcrypt_open_cmd, sizeof(dmcrypt_open_cmd),
-                       "/bin/echo '%s' | /sbin/cryptsetup -q luksOpen %s %s",
-                       passwd, loopback_device, dev_name);
+                               "/bin/echo '%s' | /sbin/cryptsetup -q luksOpen "
+                               "%s %s",
+                               passwd, loopback_device, dev_name);
        }
        free(passwd);
 
@@ -476,27 +435,25 @@ int _app2sd_dmcrypt_open_device(const char *pkgid, const char *loopback_device,
        }
 
        snprintf(buf, sizeof(buf), "/dev/mapper/%s", dev_name);
-       len = strlen(buf);
-       *dev_node = (char *)calloc(len + 1, sizeof(char));
+       *dev_node = strdup(buf);
        if (*dev_node == NULL) {
                _E("memory alloc failed");
                return APP2EXT_ERROR_OPEN_DMCRYPT_DEVICE;
        }
-       snprintf(*dev_node, len + 1, "%s", buf);
 
        return ret;
 }
 
 int _app2sd_dmcrypt_close_device(const char *pkgid, uid_t uid)
 {
-       int ret = APP2EXT_SUCCESS;
-       char dev_node[BUF_SIZE] = { '\0' };
-       char dmcrypt_close_cmd[BUF_SIZE] = { '\0' };
-       char err_buf[BUF_SIZE] = { '\0' };
-       char *t_dev_node = NULL;
+       int ret;
+       char dev_node[BUF_SIZE];
+       char dmcrypt_close_cmd[BUF_SIZE];
+       char err_buf[BUF_SIZE];
+       char *t_dev_node;
 
        if (pkgid == NULL) {
-               _E("invalid argument\n");
+               _E("invalid argument");
                return APP2EXT_ERROR_INVALID_ARGUMENTS;
        }
 
@@ -507,16 +464,14 @@ int _app2sd_dmcrypt_close_device(const char *pkgid, uid_t uid)
        }
 
        free(t_dev_node);
-       t_dev_node = NULL;
 
        snprintf(dev_node, sizeof(dev_node), "/dev/mapper/%s_%d", pkgid, uid);
        snprintf(dmcrypt_close_cmd, sizeof(dmcrypt_close_cmd),
-               "/sbin/cryptsetup -q luksClose %s", dev_node);
+                       "/sbin/cryptsetup -q luksClose %s", dev_node);
        ret = system(dmcrypt_close_cmd);
        if (ret) {
                strerror_r(errno, err_buf, sizeof(err_buf));
-               _E("error closing dmcrypt on app2sd file,"\
-                       " error: [%s]", err_buf);
+               _E("error closing dmcrypt on app2sd file: %s", err_buf);
                return APP2EXT_ERROR_CLOSE_DMCRYPT_DEVICE;
        }
 
@@ -525,27 +480,20 @@ int _app2sd_dmcrypt_close_device(const char *pkgid, uid_t uid)
 
 char *_app2sd_find_associated_dmcrypt_device_node(const char *pkgid, uid_t uid)
 {
-       char *dev_node = NULL;
-       char buf[BUF_SIZE] = { 0, };
-       int len = 0;
+       char *dev_node;
+       char buf[BUF_SIZE];
 
        snprintf(buf, sizeof(buf), "/dev/mapper/%s_%d", pkgid, uid);
-       len = strlen(buf);
-       dev_node = (char *)calloc(len + 1, sizeof(char));
-       if (dev_node == NULL) {
-               _E("memory alloc failed");
-               return NULL;
-       }
-       snprintf(dev_node, len + 1, "%s", buf);
-
-       if (access(dev_node, F_OK) == 0) {
+       if (access(buf, F_OK) == 0) {
+               dev_node = strdup(buf);
+               if (dev_node == NULL) {
+                       _E("memory alloc failed");
+                       return NULL;
+               }
                _D("device_node: (%s)", dev_node);
                return dev_node;
        }
-       _D("can not access dev_node(%s), errno(%d)", dev_node, errno);
-
-       free(dev_node);
-       dev_node = NULL;
+       _D("can not access dev_node(%s), errno(%d)", buf, errno);
 
        return NULL;
 }
@@ -553,22 +501,23 @@ char *_app2sd_find_associated_dmcrypt_device_node(const char *pkgid, uid_t uid)
 char *_app2sd_dmcrypt_duplicate_encryption_setup(const char *pkgid,
                const char *temp_loopback_device, uid_t uid)
 {
-       int ret = APP2EXT_SUCCESS;
-       char *device_node = NULL;
+       int ret;
+       char *device_node;
 
        if (pkgid == NULL || temp_loopback_device == NULL) {
-               _E("invalid argument\n");
+               _E("invalid argument");
                return NULL;
        }
 
-       ret = _app2sd_dmcrypt_setup_device(pkgid, temp_loopback_device, true, uid);
+       ret = _app2sd_dmcrypt_setup_device(pkgid, temp_loopback_device, true,
+                       uid);
        if (ret) {
                _E("dmcrypt setup device error(%d)", ret);
                return NULL;
        }
 
        ret = _app2sd_dmcrypt_open_device(pkgid, temp_loopback_device, true,
-               uid, &device_node);
+                       uid, &device_node);
        if (ret) {
                _E("dmcrypt open device error");
                return NULL;
@@ -581,38 +530,39 @@ char *_app2sd_dmcrypt_duplicate_encryption_setup(const char *pkgid,
 int _app2sd_create_loopback_device(const char *pkgid,
                const char *loopback_device, int size)
 {
-       int ret = APP2EXT_SUCCESS;
-       char command[FILENAME_MAX] = { 0, };
-       char buff[BUF_SIZE] = { 0, };
-       FILE *fp = NULL;
-
-       if (NULL == pkgid || size <= 0) {
+       int ret;
+       char command[FILENAME_MAX];
+       char buff[BUF_SIZE];
+       FILE *fp;
+       const char *argv[] = { "/bin/dd", "if=/dev/zero", command,
+               "bs=1M", buff, NULL };
+
+       if (pkgid == NULL || size <= 0) {
                _E("invalid argument");
                return APP2EXT_ERROR_INVALID_ARGUMENTS;
        }
-       snprintf(command, sizeof(command), "of=%s", loopback_device);
-       snprintf(buff, sizeof(buff), "count=%d", size);
-
-       const char *argv1[] = { "/bin/dd", "if=/dev/zero",
-               command, "bs=1M", buff, NULL };
 
-       if ((fp = fopen(loopback_device, "r+")) != NULL) {
-               _W("encrypted file already exists (%s)",
-                       loopback_device);
+       fp = fopen(loopback_device, "r+");
+       if (fp != NULL) {
+               _W("encrypted file already exists (%s)", loopback_device);
                fclose(fp);
                return APP2EXT_ERROR_PKG_EXISTS;
        }
 
-       ret = _xsystem(argv1);
+       snprintf(command, sizeof(command), "of=%s", loopback_device);
+       snprintf(buff, sizeof(buff), "count=%d", size);
+
+       ret = _xsystem(argv);
        if (ret)
-               _E("command (%s) failed, ret(%d), errno(%d)", command, ret, errno);
+               _E("command (%s) failed, ret(%d), errno(%d)",
+                               command, ret, errno);
 
        return ret;
 }
 
 int _app2sd_delete_loopback_device(const char *loopback_device)
 {
-       int ret = APP2EXT_SUCCESS;
+       int ret;
 
        ret = unlink(loopback_device);
        if (ret) {
@@ -629,9 +579,10 @@ int _app2sd_delete_loopback_device(const char *loopback_device)
 
 int _app2sd_create_file_system(const char *device_path)
 {
-       int ret = APP2EXT_SUCCESS;
-       FILE *fp = NULL;
-       char err_buf[1024] = {0,};
+       int ret;
+       FILE *fp;
+       char err_buf[1024];
+       const char *argv[] = { "/sbin/mkfs.ext4", device_path, NULL };
 
        if (device_path == NULL) {
                _E("invalid param");
@@ -639,21 +590,19 @@ int _app2sd_create_file_system(const char *device_path)
        }
 
        /* Format the filesystem [create a filesystem]*/
-       const char *argv[] = { "/sbin/mkfs.ext4", device_path, NULL };
        fp = fopen(device_path, "r+");
        if (fp == NULL) {
                strerror_r(errno, err_buf, sizeof(err_buf));
                _E("unable to access (%s) error is (%d, %s)",
-                    device_path, errno, err_buf);
+                               device_path, errno, err_buf);
                return APP2EXT_ERROR_ACCESS_FILE;
-       } else {
-               fclose(fp);
        }
+       fclose(fp);
+
        ret = _xsystem(argv);
        if (ret) {
                strerror_r(errno, err_buf, sizeof(err_buf));
-               _E("creating file system failed, error is (%s)",
-                    err_buf);
+               _E("creating file system failed, error is (%s)", err_buf);
                return APP2EXT_ERROR_CREATE_FS;
        }
        return ret;
@@ -662,14 +611,14 @@ int _app2sd_create_file_system(const char *device_path)
 static int _app2sd_create_dir_with_link(const char *application_path,
                const char *pkgid, const char *dir_name, uid_t uid)
 {
-       int ret = APP2EXT_SUCCESS;
-       char app_dir_mmc_path[FILENAME_MAX] = { 0, };
-       char app_dir_path[FILENAME_MAX] = { 0, };
+       int ret;
+       char app_dir_mmc_path[FILENAME_MAX];
+       char app_dir_path[FILENAME_MAX];
 
        snprintf(app_dir_mmc_path, sizeof(app_dir_mmc_path), "%s/.mmc/%s",
-               application_path, dir_name);
+                       application_path, dir_name);
        snprintf(app_dir_path, sizeof(app_dir_path), "%s/%s",
-               application_path, dir_name);
+                       application_path, dir_name);
 
        ret = _app2sd_make_directory(app_dir_mmc_path, uid);
        if (ret) {
@@ -677,14 +626,12 @@ static int _app2sd_create_dir_with_link(const char *application_path,
                return APP2EXT_ERROR_CREATE_DIRECTORY;
        }
 
-       if ((ret = symlink(app_dir_mmc_path,
-               app_dir_path)) < 0) {
+       ret = symlink(app_dir_mmc_path, app_dir_path);
+       if (ret < 0) {
                if (errno == EEXIST) {
-                       _D("file with symlink name present (%s)",
-                               app_dir_path);
+                       _D("file with symlink name present (%s)", app_dir_path);
                } else {
-                       _E("symbolic link creation "
-                               "failed, error no is (%d)", errno);
+                       _E("symbolic link creation failed, error: %d", errno);
                        return APP2EXT_ERROR_CREATE_SYMLINK;
                }
        }
@@ -695,21 +642,19 @@ static int _app2sd_create_dir_with_link(const char *application_path,
 static int _app2sd_create_directory_entry(const char *application_path,
                const char *pkgid, GList *dir_list, uid_t uid)
 {
-       int ret = APP2EXT_SUCCESS;
-       GList *list = NULL;
-       app2ext_dir_details *dir_detail = NULL;
+       int ret;
+       GList *list;
+       app2ext_dir_details *dir_detail;
 
-       list = g_list_first(dir_list);
-       while (list) {
+       for (list = g_list_first(dir_list); list; list = g_list_next(list)) {
                dir_detail = (app2ext_dir_details *)list->data;
-               if (dir_detail && dir_detail->name
-                       && dir_detail->type == APP2EXT_DIR_RO) {
-                       ret = _app2sd_create_dir_with_link(application_path,
+               if (dir_detail == NULL || dir_detail->name == NULL ||
+                               dir_detail->type != APP2EXT_DIR_RO)
+                       continue;
+               ret = _app2sd_create_dir_with_link(application_path,
                                pkgid, dir_detail->name, uid);
-                       if (ret)
-                               return ret;
-               }
-               list = g_list_next(list);
+               if (ret)
+                       return ret;
        }
        return APP2EXT_SUCCESS;
 }
@@ -718,24 +663,23 @@ int _app2sd_mount_app_content(const char *application_path, const char *pkgid,
                const char *dev, int mount_type, GList *dir_list,
                app2sd_cmd cmd, uid_t uid)
 {
-       int ret = APP2EXT_SUCCESS;
-       int fd = -1;
-       char app_mmc_path[FILENAME_MAX] = { 0, };
-       char temp_path[FILENAME_MAX] = { 0, };
-       char err_buf[1024] = {0,};
+       int ret;
+       int fd;
+       char app_mmc_path[FILENAME_MAX];
+       char temp_path[FILENAME_MAX];
+       char err_buf[1024];
        struct timespec time = {
                .tv_sec = 0,
                .tv_nsec = 1000 * 1000 * 200
        };
 
-       if (NULL == dev) {
-               _E("input param is NULL (%s)",
-                            dev);
+       if (dev == NULL) {
+               _E("input param is NULL (%s)", dev);
                return APP2EXT_ERROR_INVALID_ARGUMENTS;
        }
 
        /* check directory existence */
-       fd = open(application_path, O_RDONLY|O_DIRECTORY);
+       fd = open(application_path, O_RDONLY | O_DIRECTORY);
        if (fd < 0) {
                _E("path(%s) error(%d)", application_path, errno);
                return APP2EXT_ERROR_OPEN_DIR;
@@ -743,8 +687,8 @@ int _app2sd_mount_app_content(const char *application_path, const char *pkgid,
        close(fd);
 
        snprintf(app_mmc_path, sizeof(app_mmc_path), "%s/.mmc",
-               application_path);
-       fd = open(app_mmc_path, O_RDONLY|O_DIRECTORY);
+                       application_path);
+       fd = open(app_mmc_path, O_RDONLY | O_DIRECTORY);
        if (fd < 0) {
                _E("path(%s) error(%d)", app_mmc_path, errno);
                return APP2EXT_ERROR_OPEN_DIR;
@@ -756,47 +700,48 @@ int _app2sd_mount_app_content(const char *application_path, const char *pkgid,
 
        switch (mount_type) {
        case MOUNT_TYPE_RD:
-               if ((ret = mount(dev, app_mmc_path, FS_TYPE,
-                       MS_MGC_VAL | MS_RDONLY | MS_NOSUID, NULL)) < 0) {
-                       _E("read only mount failed, " \
-                               "errono is (%d), " \
-                               "dev is (%s) path is (%s)",
-                               errno, dev, app_mmc_path);
+               ret = mount(dev, app_mmc_path, FS_TYPE,
+                               MS_MGC_VAL | MS_RDONLY | MS_NOSUID, NULL);
+               if (ret < 0) {
+                       _E("read only mount failed, errono is (%d), "
+                                       "dev is (%s) path is (%s)",
+                                       errno, dev, app_mmc_path);
                        ret = APP2EXT_ERROR_MOUNT;
                }
                break;
        case MOUNT_TYPE_RW:
-               if ((ret = mount(dev, app_mmc_path, FS_TYPE,
-                       MS_MGC_VAL | MS_NOSUID, NULL)) < 0) {
-                       _E("read write mount failed, " \
-                               "errono is (%d)", errno);
+               ret = mount(dev, app_mmc_path, FS_TYPE, MS_MGC_VAL | MS_NOSUID,
+                               NULL);
+               if (ret < 0) {
+                       _E("read write mount failed, errono is (%d)", errno);
                        ret = APP2EXT_ERROR_MOUNT;
                }
                break;
        case MOUNT_TYPE_RW_NOEXEC:
-               if ((ret = mount(dev, app_mmc_path, FS_TYPE,
-                       MS_MGC_VAL | MS_NOEXEC | MS_NOSUID, NULL)) < 0) {
-                       _E("rwnx mount failed " \
-                               "errono is (%d)", errno);
+               ret = mount(dev, app_mmc_path, FS_TYPE,
+                               MS_MGC_VAL | MS_NOEXEC | MS_NOSUID, NULL);
+               if (ret < 0) {
+                       _E("rwnx mount failed errono is (%d)", errno);
                        ret = APP2EXT_ERROR_MOUNT;
                }
                break;
        case MOUNT_TYPE_RD_REMOUNT:
-               if ((ret = mount(dev, app_mmc_path, FS_TYPE,
-                       MS_MGC_VAL | MS_RDONLY | MS_REMOUNT | MS_NOSUID,
-                       NULL)) < 0) {
-                       _E("read remount failed "
-                               "errono is (%d)", errno);
+               ret = mount(dev, app_mmc_path, FS_TYPE,
+                               MS_MGC_VAL | MS_RDONLY | MS_REMOUNT | MS_NOSUID,
+                               NULL);
+               if (ret < 0) {
+                       _E("read remount failed errono is (%d)", errno);
                        ret = APP2EXT_ERROR_MOUNT;
                }
                break;
        case MOUNT_TYPE_RW_REMOUNT:
-               if ((ret = mount(dev, app_mmc_path, FS_TYPE,
-                       MS_MGC_VAL | MS_REMOUNT | MS_NOSUID, NULL)) < 0) {
-                       _E("read write remount failed "
-                               "errono(%d), errstr(%s)", errno,
-                               strerror_r(errno, err_buf, sizeof(err_buf)));
-                               ret = APP2EXT_ERROR_MOUNT;
+               ret = mount(dev, app_mmc_path, FS_TYPE,
+                               MS_MGC_VAL | MS_REMOUNT | MS_NOSUID, NULL);
+               strerror_r(errno, err_buf, sizeof(err_buf));
+               if (ret < 0) {
+                       _E("read write remount failed errono(%d), errstr(%s)",
+                                       errno, err_buf);
+                       ret = APP2EXT_ERROR_MOUNT;
                }
                break;
        default:
@@ -805,13 +750,13 @@ int _app2sd_mount_app_content(const char *application_path, const char *pkgid,
        }
 
        if (cmd == APP2SD_PRE_INSTALL || cmd == APP2SD_MOVE_APP_TO_MMC ||
-               cmd == APP2SD_PRE_UPGRADE) {
+                       cmd == APP2SD_PRE_UPGRADE) {
                ret = _app2sd_create_directory_entry(application_path,
-                       pkgid, dir_list, uid);
+                               pkgid, dir_list, uid);
        }
 
        if (mount_type != MOUNT_TYPE_RD &&
-               mount_type != MOUNT_TYPE_RD_REMOUNT) {
+                       mount_type != MOUNT_TYPE_RD_REMOUNT) {
                /* change lost+found permission */
                snprintf(temp_path, sizeof(temp_path), "%s/lost+found",
                                app_mmc_path);
@@ -827,16 +772,17 @@ int _app2sd_mount_app_content(const char *application_path, const char *pkgid,
 
 int _app2sd_unmount_app_content(const char *application_path)
 {
-       int ret = APP2EXT_SUCCESS;
-       char app_mmc_path[FILENAME_MAX] = { 0, };
-       char err_buf[1024] = {0,};
+       int ret;
+       char app_mmc_path[FILENAME_MAX];
+       char err_buf[1024];
 
        snprintf(app_mmc_path, sizeof(app_mmc_path), "%s/.mmc",
-               application_path);
-       if ((ret = umount(app_mmc_path)) < 0) {
+                       application_path);
+       ret = umount(app_mmc_path);
+       if (ret < 0) {
                strerror_r(errno, err_buf, sizeof(err_buf));
                _D("unable to umount the dir, ret(%d) error(%d, %s)",
-                       ret, errno, err_buf);
+                               ret, errno, err_buf);
        }
 
        return ret;
@@ -844,18 +790,18 @@ int _app2sd_unmount_app_content(const char *application_path)
 
 static int _app2sd_move_to_archive(const char *src_path, const char *arch_path)
 {
-       int ret = APP2EXT_SUCCESS;
+       int ret;
+       char err_buf[1024];
 
        ret = _app2sd_copy_dir(src_path, arch_path);
        if (ret && ret != APP2EXT_ERROR_ACCESS_FILE) {
-               char err_buf[1024] = {0,};
                strerror_r(errno, err_buf, sizeof(err_buf));
                _E("unable to copy from (%s) to (%s), err is (%s)",
-                       src_path, arch_path, err_buf);
+                               src_path, arch_path, err_buf);
                return APP2EXT_ERROR_MOVE;
        }
 
-       ret = _app2sd_delete_directory((char *)src_path);
+       ret = _app2sd_delete_directory(src_path);
        if (ret) {
                _E("unable to delete (%s)", src_path);
                return APP2EXT_ERROR_DELETE_DIRECTORY;
@@ -867,56 +813,54 @@ static int _app2sd_move_to_archive(const char *src_path, const char *arch_path)
 static int _app2sd_move_app_to_external(const char *pkgid, GList *dir_list,
                uid_t uid, char *mmc_path, char **image_path)
 {
-       int ret = APP2EXT_SUCCESS;
+       int ret;
        mode_t mode = DIR_PERMS;
-       char temp_dir_path[FILENAME_MAX] = { 0, };
-       char app_mmc_path[FILENAME_MAX] = { 0, };
-       char app_archive_path[FILENAME_MAX] = { 0, };
-       char application_path[FILENAME_MAX] = { 0, };
-       char loopback_device[FILENAME_MAX] = { 0, };
+       char temp_dir_path[FILENAME_MAX];
+       char app_mmc_path[FILENAME_MAX];
+       char app_archive_path[FILENAME_MAX];
+       char application_path[FILENAME_MAX];
+       char loopback_device[FILENAME_MAX];
        unsigned long long total_size = 0;
-       int reqd_size = 0;
-       int reqd_disk_size = 0;
-       char *device_node = NULL;
+       int reqd_size;
+       int reqd_disk_size;
+       char *device_node;
 #if !defined(TIZEN_FEATURE_APP2SD_DMCRYPT_ENCRYPTION)
-       char *devi = NULL;
+       char *devi;
 #endif
-       int free_mmc_mem = 0;
-       FILE *fp = NULL;
-       GList *list = NULL;
-       app2ext_dir_details *dir_detail = NULL;
-       char err_buf[1024] = { 0,};
-       char *encoded_id = NULL;
-
-       encoded_id = _app2sd_get_encoded_name(pkgid, uid);
-       if (encoded_id == NULL)
-               return APP2EXT_ERROR_MEMORY_ALLOC_FAILED;
-
-       snprintf(loopback_device, sizeof(loopback_device), "%s/%s/%s",
-                       mmc_path, EXTIMG_DIR, encoded_id);
-       free(encoded_id);
+       int free_mmc_mem;
+       FILE *fp;
+       GList *list;
+       app2ext_dir_details *dir_detail;
+       char err_buf[1024];
+
+       ret = _app2sd_get_loopback_device_path(mmc_path, pkgid, uid,
+                       loopback_device, sizeof(loopback_device));
+       if (ret)
+               return ret;
+
        _app2sd_set_application_path(pkgid, uid, application_path,
-               sizeof(application_path));
+                       sizeof(application_path));
 
        /* check whether application is in external memory or not */
        fp = fopen(loopback_device, "r+");
        if (fp != NULL) {
-               _W("Already %s entry is present in the SD Card, " \
-                       "delete entry and go on without return", pkgid);
+               _W("Already %s entry is present in the SD Card, "
+                               "delete entry and go on without return", pkgid);
                fclose(fp);
-               _app2sd_force_clean(pkgid, application_path, loopback_device, uid);
+               _app2sd_force_clean(pkgid, application_path, loopback_device,
+                               uid);
        }
 
        snprintf(app_mmc_path, sizeof(app_mmc_path), "%s/.mmc",
-               application_path);
+                       application_path);
        snprintf(app_archive_path, sizeof(app_archive_path), "%s/.archive",
-               application_path);
+                       application_path);
 
        ret = mkdir(app_mmc_path, mode);
        if (ret) {
                if (errno != EEXIST) {
-                       _E("unable to create directory for archiving," \
-                               " error(%d)", errno);
+                       _E("unable to create directory for archiving,"
+                                       " error(%d)", errno);
                        return APP2EXT_ERROR_CREATE_DIRECTORY;
                }
        }
@@ -924,27 +868,21 @@ static int _app2sd_move_app_to_external(const char *pkgid, GList *dir_list,
        ret = mkdir(app_archive_path, mode);
        if (ret) {
                if (errno != EEXIST) {
-                       _E("unable to create directory for archiving," \
-                               " error(%d)", errno);
+                       _E("unable to create directory for archiving,"
+                                       " error(%d)", errno);
                        return APP2EXT_ERROR_CREATE_DIRECTORY;
                }
        }
 
-       list = g_list_first(dir_list);
-       while (list) {
+       for (list = g_list_first(dir_list); list; list = g_list_next(list)) {
                dir_detail = (app2ext_dir_details *)list->data;
-               if (dir_detail && dir_detail->name
-                       && dir_detail->type == APP2EXT_DIR_RO) {
-                       memset(temp_dir_path, '\0', sizeof(temp_dir_path));
-                       snprintf(temp_dir_path, sizeof(temp_dir_path),
-                                "%s/%s", application_path,
-                                dir_detail->name);
-                       _D("cal size of app dirs, temp_dir_path(%s)",
-                               temp_dir_path);
-                       total_size +=
-                           _app2sd_calculate_dir_size(temp_dir_path);
-               }
-               list = g_list_next(list);
+               if (dir_detail == NULL || dir_detail->name == NULL ||
+                               dir_detail->type != APP2EXT_DIR_RO)
+                       continue;
+               snprintf(temp_dir_path, sizeof(temp_dir_path), "%s/%s",
+                               application_path, dir_detail->name);
+               _D("cal size of app dirs, temp_dir_path(%s)", temp_dir_path);
+               total_size += _app2sd_calculate_dir_size(temp_dir_path);
        }
 
        reqd_size = ((total_size) / (1024 * 1024)) + 2;
@@ -960,13 +898,13 @@ static int _app2sd_move_app_to_external(const char *pkgid, GList *dir_list,
         * required size + 5MB, return error
         */
        if (reqd_disk_size > free_mmc_mem) {
-               _E("insufficient memory in MMC for application installation (%d)",
-                       ret);
+               _E("insufficient memory in MMC for "
+                               "application installation (%d)", ret);
                return APP2EXT_ERROR_MMC_INSUFFICIENT_MEMORY;
        }
        /* create a loopback device */
        ret = _app2sd_create_loopback_device(pkgid, loopback_device,
-               (reqd_disk_size + PKG_BUF_SIZE));
+                       (reqd_disk_size + PKG_BUF_SIZE));
        if (ret) {
                _E("loopback node creation failed");
                return ret;
@@ -980,7 +918,7 @@ static int _app2sd_move_app_to_external(const char *pkgid, GList *dir_list,
        }
 
        ret = _app2sd_dmcrypt_open_device(pkgid, loopback_device, false,
-               uid, &device_node);
+                       uid, &device_node);
        if (ret) {
                _E("dmcrypt open device error");
                return APP2EXT_ERROR_OPEN_DMCRYPT_DEVICE;
@@ -988,22 +926,22 @@ static int _app2sd_move_app_to_external(const char *pkgid, GList *dir_list,
 #else
        /* perform loopback encryption setup */
        device_node = _app2sd_do_loopback_encryption_setup(pkgid,
-               loopback_device, uid);
+                       loopback_device, uid);
        if (!device_node) {
                _E("loopback encryption setup failed");
                return APP2EXT_ERROR_DO_LOSETUP;
        }
        _D("device_node (%s)", device_node);
-       /* check whether loopback device is associated with device node or not */
+       /* check whether loopback device is associated with
+        * device node or not
+        */
        devi = _app2sd_find_associated_device_node(loopback_device);
        if (devi == NULL) {
                _E("finding associated device node failed");
                ret = APP2EXT_ERROR_DO_LOSETUP;
                goto ERR;
-       } else {
-               free(devi);
-               devi = NULL;
        }
+       free(devi);
 #endif
        /* format the loopback file system */
        ret = _app2sd_create_file_system(device_node);
@@ -1013,78 +951,62 @@ static int _app2sd_move_app_to_external(const char *pkgid, GList *dir_list,
                goto ERR;
        }
 
-       list = g_list_first(dir_list);
-       while (list) {
+       for (list = g_list_first(dir_list); list; list = g_list_next(list)) {
                dir_detail = (app2ext_dir_details *)list->data;
-               if (dir_detail && dir_detail->name
-                       && dir_detail->type == APP2EXT_DIR_RO) {
-                       memset(temp_dir_path, '\0', FILENAME_MAX);
-                       snprintf(temp_dir_path, sizeof(temp_dir_path),
-                                "%s/%s", application_path,
-                               dir_detail->name);
-                       _D("app2archive, temp_dir_path(%s)",
-                               temp_dir_path);
-                       ret = _app2sd_move_to_archive(temp_dir_path,
-                               app_archive_path);
-                       if (ret) {
-                               if (ret == APP2EXT_ERROR_ACCESS_FILE) {
-                                       _E("unable to access (%s)",
-                                               temp_dir_path);
-                               } else {
-                                       _E("unable to copy from (%s) to (%s)",
-                                            temp_dir_path,
-                                            app_archive_path);
-                               }
-                               goto ERR;
+               if (dir_detail == NULL || dir_detail->name == NULL ||
+                               dir_detail->type != APP2EXT_DIR_RO)
+                       continue;
+               snprintf(temp_dir_path, sizeof(temp_dir_path), "%s/%s",
+                               application_path, dir_detail->name);
+               _D("app2archive, temp_dir_path(%s)", temp_dir_path);
+               ret = _app2sd_move_to_archive(temp_dir_path, app_archive_path);
+               if (ret) {
+                       if (ret == APP2EXT_ERROR_ACCESS_FILE) {
+                               _E("unable to access (%s)", temp_dir_path);
+                       } else {
+                               _E("unable to copy from (%s) to (%s)",
+                                               temp_dir_path,
+                                               app_archive_path);
                        }
+                       goto ERR;
                }
-               list = g_list_next(list);
        }
 
-       /* mount the loopback encrypted pseudo device on application installation path
-        * as with Read Write permission
+       /* mount the loopback encrypted pseudo device on
+        * application installation path as with Read Write permission
         */
        ret = _app2sd_mount_app_content(application_path, pkgid, device_node,
-               MOUNT_TYPE_RW, dir_list, APP2SD_MOVE_APP_TO_MMC, uid);
+                       MOUNT_TYPE_RW, dir_list, APP2SD_MOVE_APP_TO_MMC, uid);
        if (ret) {
                _E("mount failed");
                goto ERR;
        }
 
-       list = g_list_first(dir_list);
-       while (list) {
+       for (list = g_list_first(dir_list); list; list = g_list_next(list)) {
                dir_detail = (app2ext_dir_details *)list->data;
-               if (dir_detail && dir_detail->name
-                       && dir_detail->type == APP2EXT_DIR_RO) {
-                       memset(temp_dir_path, '\0', sizeof(temp_dir_path));
-                       snprintf(temp_dir_path, sizeof(temp_dir_path),
-                               "%s/%s", app_archive_path,
-                               dir_detail->name);
-                       _D("archive2mmc, temp_dir_path(%s)",
-                               temp_dir_path);
-                       ret = _app2sd_copy_dir(temp_dir_path,
-                               app_mmc_path);
-                       if (ret) {
-                               if (ret == APP2EXT_ERROR_ACCESS_FILE) {
-                                       _E("unable to access (%s)",
-                                               temp_dir_path);
-                               } else {
-                                       strerror_r(errno,
-                                               err_buf, sizeof(err_buf));
-                                       _E("unable to copy from (%s) to (%s)," \
-                                               " error is (%s)",
-                                               temp_dir_path,
+               if (dir_detail == NULL || dir_detail->name == NULL ||
+                               dir_detail->type != APP2EXT_DIR_RO)
+                       continue;
+               snprintf(temp_dir_path, sizeof(temp_dir_path), "%s/%s",
+                               app_archive_path, dir_detail->name);
+               _D("archive2mmc, temp_dir_path(%s)", temp_dir_path);
+               ret = _app2sd_copy_dir(temp_dir_path, app_mmc_path);
+               if (ret) {
+                       if (ret == APP2EXT_ERROR_ACCESS_FILE) {
+                               _E("unable to access (%s)", temp_dir_path);
+                       } else {
+                               strerror_r(errno, err_buf, sizeof(err_buf));
+                               _E("unable to copy from (%s) to (%s),"
+                                               " error is (%s)", temp_dir_path,
                                                app_mmc_path, err_buf);
-                               }
-                               goto ERR;
-                       }
-                       ret = _app2sd_delete_directory(temp_dir_path);
-                       if (ret) {
-                               _E("unable to delete (%s)", temp_dir_path);
-                               goto ERR;
                        }
+                       goto ERR;
+               }
+               ret = _app2sd_delete_directory(temp_dir_path);
+               if (ret) {
+                       _E("unable to delete (%s)", temp_dir_path);
+                       goto ERR;
                }
-               list = g_list_next(list);
        }
 
        ret = _app2sd_delete_directory(app_archive_path);
@@ -1098,10 +1020,8 @@ static int _app2sd_move_app_to_external(const char *pkgid, GList *dir_list,
        return APP2EXT_SUCCESS;
 
 ERR:
-       if (device_node) {
+       if (device_node)
                free(device_node);
-               device_node = NULL;
-       }
 
        *image_path = NULL;
        return ret;
@@ -1110,30 +1030,28 @@ ERR:
 static int _app2sd_move_app_to_internal(const char *pkgid, GList *dir_list,
                uid_t uid, char *mmc_path)
 {
-       int ret = APP2EXT_SUCCESS;
+       int ret;
        mode_t mode = DIR_PERMS;
-       char temp_dir_path[FILENAME_MAX] = { 0, };
-       char app_mmc_path[FILENAME_MAX] = { 0, };
-       char app_archive_path[FILENAME_MAX] = { 0, };
-       char application_path[FILENAME_MAX] = { 0, };
-       char loopback_device[FILENAME_MAX] = { 0, };
-       char *device_node = NULL;
-       FILE *fp = NULL;
-       GList *list = NULL;
-       app2ext_dir_details *dir_detail = NULL;
-       int reqd_size = 0;
-       int free_internal_mem = 0;
+       char temp_dir_path[FILENAME_MAX];
+       char app_mmc_path[FILENAME_MAX];
+       char app_archive_path[FILENAME_MAX];
+       char application_path[FILENAME_MAX];
+       char loopback_device[FILENAME_MAX];
+       char *device_node;
+       FILE *fp;
+       GList *list;
+       app2ext_dir_details *dir_detail;
+       int reqd_size;
+       int free_internal_mem;
        unsigned long long temp = 0;
-       char err_buf[1024] = {0,};
-       char *encoded_id = NULL;
+       char err_buf[1024];
+       int mount_type;
 
-       encoded_id = _app2sd_get_encoded_name(pkgid, uid);
-       if (encoded_id == NULL)
-               return APP2EXT_ERROR_MEMORY_ALLOC_FAILED;
+       ret = _app2sd_get_loopback_device_path(mmc_path, pkgid, uid,
+                       loopback_device, sizeof(loopback_device));
+       if (ret)
+               return ret;
 
-       snprintf(loopback_device, sizeof(loopback_device), "%s/%s/%s",
-                       mmc_path, EXTIMG_DIR, encoded_id);
-       free(encoded_id);
        _app2sd_set_application_path(pkgid, uid, application_path,
                sizeof(application_path));
 
@@ -1143,17 +1061,14 @@ static int _app2sd_move_app_to_internal(const char *pkgid, GList *dir_list,
                _E("application (%s) is not installed on SD Card",
                     pkgid);
                return APP2EXT_ERROR_FILE_ABSENT;
-       } else {
-               fclose(fp);
-               fp = NULL;
        }
+       fclose(fp);
 
        /* find avialable free memory in the internal storage */
        ret = _app2sd_get_available_free_memory(INTERNAL_STORAGE_PATH,
-               &free_internal_mem);
+                       &free_internal_mem);
        if (ret) {
-               _E("unable to get available free memory in internal (%d)",
-                       ret);
+               _E("unable to get available free memory in internal (%d)", ret);
                return APP2EXT_ERROR_MMC_STATUS;
        }
 
@@ -1175,13 +1090,13 @@ static int _app2sd_move_app_to_internal(const char *pkgid, GList *dir_list,
        }
 
        _I("reqd size: (%d)MB, free internal mem: (%d)MB",
-               reqd_size, free_internal_mem);
+                       reqd_size, free_internal_mem);
 
        /* if avaialalbe free memory in internal storage is
         * less than required size, return error
         */
        if (reqd_size > free_internal_mem) {
-               _E("innsufficient memory in internal storage" \
+               _E("innsufficient memory in internal storage"
                        " for application installation (%d)", ret);
                return APP2EXT_ERROR_MMC_INSUFFICIENT_MEMORY;
        }
@@ -1192,136 +1107,110 @@ static int _app2sd_move_app_to_internal(const char *pkgid, GList *dir_list,
 #else
        device_node = _app2sd_find_associated_device_node(loopback_device);
 #endif
-       if (NULL == device_node) {
+       if (device_node == NULL) {
                /* do loopback setup */
 #ifdef TIZEN_FEATURE_APP2SD_DMCRYPT_ENCRYPTION
                ret = _app2sd_dmcrypt_open_device(pkgid, loopback_device,
-                       false, uid, &device_node);
+                               false, uid, &device_node);
                if (ret) {
                        _E("dmcrypt open device error(%d)", ret);
                        return APP2EXT_ERROR_OPEN_DMCRYPT_DEVICE;
                }
 #else
                device_node = _app2sd_do_loopback_encryption_setup(pkgid,
-                       loopback_device, uid);
+                               loopback_device, uid);
                if (device_node == NULL) {
                        _E("loopback encryption setup failed");
                        return APP2EXT_ERROR_DO_LOSETUP;
                }
 #endif
-               /* do mounting */
-               ret = _app2sd_mount_app_content(application_path,
-                       pkgid, device_node, MOUNT_TYPE_RW,
-                       dir_list, APP2SD_MOVE_APP_TO_PHONE, uid);
-               if (ret) {
-                       _E("mount failed");
-                       ret = APP2EXT_ERROR_MOUNT_PATH;
-                       goto ERR;
-               }
+               mount_type = MOUNT_TYPE_RW;
        } else {
-               /* do re-mounting */
-               ret = _app2sd_mount_app_content(application_path,
-                       pkgid, device_node, MOUNT_TYPE_RW_REMOUNT,
+               mount_type = MOUNT_TYPE_RW_REMOUNT;
+       }
+       /* do mounting */
+       ret = _app2sd_mount_app_content(application_path,
+                       pkgid, device_node, mount_type,
                        dir_list, APP2SD_MOVE_APP_TO_PHONE, uid);
-               if (ret) {
-                       _E("re-mount failed");
-                       ret = APP2EXT_ERROR_MOUNT_PATH;
-                       goto ERR;
-               }
+       if (ret) {
+               _E("mount failed");
+               ret = APP2EXT_ERROR_MOUNT_PATH;
+               goto ERR;
        }
 
        snprintf(app_mmc_path, sizeof(app_mmc_path), "%s/.mmc",
-               application_path);
+                       application_path);
        snprintf(app_archive_path, sizeof(app_archive_path), "%s/.archive",
-               application_path);
+                       application_path);
 
        ret = mkdir(app_archive_path, mode);
        if (ret) {
                if (errno != EEXIST) {
-                       _E("unable to create directory for archiving," \
-                               " error(%d)", errno);
+                       _E("unable to create directory for archiving,"
+                                       " error(%d)", errno);
                        ret = APP2EXT_ERROR_CREATE_DIRECTORY;
                        goto ERR;
                }
        }
 
-       list = g_list_first(dir_list);
-       while (list) {
+       for (list = g_list_first(dir_list); list; list = g_list_next(list)) {
                dir_detail = (app2ext_dir_details *)list->data;
-               if (dir_detail && dir_detail->name
-                       && dir_detail->type == APP2EXT_DIR_RO) {
-                       /* archiving code */
-                       memset(temp_dir_path, '\0', sizeof(temp_dir_path));
-                       snprintf(temp_dir_path, sizeof(temp_dir_path),
-                                "%s/%s", app_mmc_path,
-                                dir_detail->name);
-                       _D("mmc2archive, temp_dir_path(%s)", temp_dir_path);
-                       ret = _app2sd_copy_dir(temp_dir_path,
-                                    app_archive_path);
-                       if (ret) {
-                               if (ret == APP2EXT_ERROR_ACCESS_FILE) {
-                                       _E("unable to access (%s)",
-                                               temp_dir_path);
-                               } else {
-                                       strerror_r(errno,
-                                               err_buf, sizeof(err_buf));
-                                       _E("unable to copy from (%s) to (%s),"
-                                               " error is (%s)",
-                                               temp_dir_path,
+               if (dir_detail == NULL || dir_detail->name == NULL ||
+                               dir_detail->type != APP2EXT_DIR_RO)
+                       continue;
+               /* archiving code */
+               snprintf(temp_dir_path, sizeof(temp_dir_path), "%s/%s",
+                               app_mmc_path, dir_detail->name);
+               _D("mmc2archive, temp_dir_path(%s)", temp_dir_path);
+               ret = _app2sd_copy_dir(temp_dir_path, app_archive_path);
+               if (ret) {
+                       if (ret == APP2EXT_ERROR_ACCESS_FILE) {
+                               _E("unable to access (%s)", temp_dir_path);
+                       } else {
+                               strerror_r(errno, err_buf, sizeof(err_buf));
+                               _E("unable to copy from (%s) to (%s),"
+                                               "error is (%s)", temp_dir_path,
                                                app_archive_path, err_buf);
-                               }
-                               goto ERR;
                        }
-
-                       /* delete the symbolic link files [bin, lib, res]*/
-                       memset(temp_dir_path, '\0', sizeof(temp_dir_path));
-                       snprintf(temp_dir_path, sizeof(temp_dir_path),
-                                "%s/%s", application_path,
-                                dir_detail->name);
-                       _D("unlink, temp_dir_path(%s)", temp_dir_path);
-                       ret = unlink(temp_dir_path);
-                       if (ret) {
-                               if (errno == ENOENT) {
-                                       _W("directory (%s) does not exist",
-                                               temp_dir_path);
-                               } else {
-                                       _E("unable to remove the symbolic link file (%s)," \
-                                               " it is already unlinked",
+                       goto ERR;
+               }
+               /* delete the symbolic link files [bin, lib, res]*/
+               snprintf(temp_dir_path, sizeof(temp_dir_path), "%s/%s",
+                               application_path, dir_detail->name);
+               _D("unlink, temp_dir_path(%s)", temp_dir_path);
+               ret = unlink(temp_dir_path);
+               if (ret) {
+                       if (errno == ENOENT) {
+                               _W("(%s) does not exist", temp_dir_path);
+                       } else {
+                               _E("unable to remove the symbolic link file "
+                                               "(%s), it is already unlinked",
                                                temp_dir_path);
-                                       goto ERR;
-                               }
+                               goto ERR;
                        }
-
-                       /* Copy content to destination */
-                       memset(temp_dir_path, '\0', sizeof(temp_dir_path));
-                       snprintf(temp_dir_path, sizeof(temp_dir_path),
-                               "%s/%s", app_archive_path,
-                               dir_detail->name);
-                       _D("archive2app, temp_dir_path(%s)", temp_dir_path);
-                       ret = _app2sd_copy_dir(temp_dir_path, application_path);
-                       if (ret) {
-                               if (ret == APP2EXT_ERROR_ACCESS_FILE) {
-                                       _E("unable to access (%s)",
-                                               temp_dir_path);
-                               } else {
-                                       strerror_r(errno,
-                                               err_buf, sizeof(err_buf));
-                                       _E("unable to copy from (%s) to (%s) " \
-                                               ", error is (%s)",
-                                               temp_dir_path,
+               }
+               /* Copy content to destination */
+               snprintf(temp_dir_path, sizeof(temp_dir_path), "%s/%s",
+                               app_archive_path, dir_detail->name);
+               _D("archive2app, temp_dir_path(%s)", temp_dir_path);
+               ret = _app2sd_copy_dir(temp_dir_path, application_path);
+               if (ret) {
+                       if (ret == APP2EXT_ERROR_ACCESS_FILE) {
+                               _E("unable to access (%s)", temp_dir_path);
+                       } else {
+                               strerror_r(errno, err_buf, sizeof(err_buf));
+                               _E("unable to copy from (%s) to (%s), "
+                                               "error is (%s)", temp_dir_path,
                                                application_path, err_buf);
-                               }
-                               goto ERR;
                        }
+                       goto ERR;
                }
-               list = g_list_next(list);
        }
 
        _D("copying file completed");
        ret = _app2sd_unmount_app_content(application_path);
        if (ret)
-               _E("unable to unmount SD directory for app (%s)",
-                    pkgid);
+               _E("unable to unmount SD directory for app (%s)", pkgid);
 
 #ifdef TIZEN_FEATURE_APP2SD_DMCRYPT_ENCRYPTION
        ret = _app2sd_dmcrypt_close_device(pkgid, uid);
@@ -1330,14 +1219,12 @@ static int _app2sd_move_app_to_internal(const char *pkgid, GList *dir_list,
 #else
        ret = _app2sd_remove_loopback_encryption_setup(loopback_device);
        if (ret)
-               _E("unable to detach loopback setup for (%s)",
-                    pkgid);
+               _E("unable to detach loopback setup for (%s)", pkgid);
 #endif
 
        ret = _app2sd_delete_loopback_device(loopback_device);
        if (ret)
-               _E("unable to delete the loopback device for (%s)",
-                    pkgid);
+               _E("unable to delete the loopback device for (%s)", pkgid);
 
        ret = _app2sd_delete_directory(app_mmc_path);
        if (ret)
@@ -1359,10 +1246,8 @@ static int _app2sd_move_app_to_internal(const char *pkgid, GList *dir_list,
        return APP2EXT_SUCCESS;
 
 ERR:
-       if (device_node) {
+       if (device_node)
                free(device_node);
-               device_node = NULL;
-       }
 
        return ret;
 }
@@ -1370,12 +1255,12 @@ ERR:
 int _app2sd_usr_move_app(const char *pkgid, app2ext_move_type move_type,
                GList *dir_list, uid_t uid, char *mmc_path, char **image_path)
 {
-       int ret = APP2EXT_SUCCESS;
+       int ret;
 
        switch (move_type) {
        case APP2EXT_MOVE_TO_EXT:
                ret = _app2sd_move_app_to_external(pkgid, dir_list,
-                       uid, mmc_path, image_path);
+                               uid, mmc_path, image_path);
                if (ret) {
                        _E("move app to external memory failed(%d)", ret);
                        return ret;
@@ -1383,7 +1268,7 @@ int _app2sd_usr_move_app(const char *pkgid, app2ext_move_type move_type,
                break;
        case APP2EXT_MOVE_TO_PHONE:
                ret = _app2sd_move_app_to_internal(pkgid, dir_list,
-                       uid, mmc_path);
+                               uid, mmc_path);
                if (ret) {
                        _E("move app to internal memory failed(%d)", ret);
                        return ret;
@@ -1399,56 +1284,50 @@ int _app2sd_usr_move_app(const char *pkgid, app2ext_move_type move_type,
 
 int _app2sd_copy_ro_content(const char *src, const char *dest, GList *dir_list)
 {
-       char path[FILENAME_MAX] = { 0, };
-       int ret = APP2EXT_SUCCESS;
-       GList *list = NULL;
-       app2ext_dir_details *dir_detail = NULL;
+       char path[FILENAME_MAX];
+       int ret;
+       GList *list;
+       app2ext_dir_details *dir_detail;
 
-       list = g_list_first(dir_list);
-       while (list) {
+       for (list = g_list_first(dir_list); list; list = g_list_next(list)) {
                dir_detail = (app2ext_dir_details *)list->data;
-               if (dir_detail && dir_detail->name
-                       && dir_detail->type == APP2EXT_DIR_RO) {
-                       memset((void *)&path, '\0', sizeof(path));
-                       snprintf(path, sizeof(path), "%s/%s", src,
-                                dir_detail->name);
-                       ret = _app2sd_copy_dir(path, dest);
-                       if (ret) {
-                               if (ret == APP2EXT_ERROR_ACCESS_FILE) {
-                                       _E("unable to access (%s)", path);
-                               } else {
-                                       _E("unable to copy from (%s) " \
+               if (dir_detail == NULL || dir_detail->name == NULL ||
+                               dir_detail->type != APP2EXT_DIR_RO)
+                       continue;
+
+               snprintf(path, sizeof(path), "%s/%s", src, dir_detail->name);
+               ret = _app2sd_copy_dir(path, dest);
+               if (ret) {
+                       if (ret == APP2EXT_ERROR_ACCESS_FILE) {
+                               _E("unable to access (%s)", path);
+                       } else {
+                               _E("unable to copy from (%s) "
                                                "to (%s), errno is (%d)",
                                                path, dest, errno);
-                                       return APP2EXT_ERROR_MOVE;
-                               }
+                               return APP2EXT_ERROR_MOVE;
                        }
                }
-               list = g_list_next(list);
        }
 
        return APP2EXT_SUCCESS;
 }
 
-int _app2sd_duplicate_device(const char *pkgid,
-               const char *loopback_device,
-               const char *temp_pkgid,
-               const char *temp_application_path,
-               const char *temp_loopback_device,
-               GList *dir_list, char *dev_node, int size,
-               uid_t uid)
+int _app2sd_duplicate_device(const char *pkgid, const char *loopback_device,
+               const char *temp_pkgid, const char *temp_application_path,
+               const char *temp_loopback_device, GList *dir_list,
+               char *dev_node, int size, uid_t uid)
 {
-       int ret = 0;
-       int err_res = 0;
+       int ret;
+       int err_res;
 #if !defined(TIZEN_FEATURE_APP2SD_DMCRYPT_ENCRYPTION)
-       char *devi = NULL;
-       char *result = NULL;
-       char *passwd = NULL;
+       char *devi;
+       char *result;
+       char *passwd;
 #endif
 
        /* create a new loopback device */
        ret = _app2sd_create_loopback_device(temp_pkgid,
-               temp_loopback_device, (size + PKG_BUF_SIZE));
+                       temp_loopback_device, (size + PKG_BUF_SIZE));
        if (ret) {
                _E("package already present");
                return ret;
@@ -1457,7 +1336,7 @@ int _app2sd_duplicate_device(const char *pkgid,
        /* perform loopback encryption setup */
 #ifdef TIZEN_FEATURE_APP2SD_DMCRYPT_ENCRYPTION
        dev_node = _app2sd_dmcrypt_duplicate_encryption_setup(pkgid,
-               temp_loopback_device, uid);
+                       temp_loopback_device, uid);
        if (!dev_node) {
                _E("dmcrypt duplicate encryption setup failed");
                _app2sd_delete_loopback_device(temp_loopback_device);
@@ -1471,33 +1350,32 @@ int _app2sd_duplicate_device(const char *pkgid,
                return APP2EXT_ERROR_DB_INITIALIZE;
        }
 
-       if ((passwd = _app2sd_get_password_from_db(pkgid, uid)) == NULL) {
-               passwd = (char *)_app2sd_generate_password();
-               if (NULL == passwd) {
+       passwd = _app2sd_get_password_from_db(pkgid, uid);
+       if (passwd == NULL) {
+               passwd = _app2sd_generate_password();
+               if (passwd == NULL) {
                        _E("unable to generate password");
                        return APP2EXT_ERROR_PASSWD_GENERATION;
                } else {
-                       if ((ret = _app2sd_set_info_in_db(pkgid,
-                               passwd, loopback_device, uid)) < 0) {
+                       ret = _app2sd_set_info_in_db(pkgid, passwd,
+                                       loopback_device, uid);
+                       if (ret < 0) {
                                _E("unable to save info");
                                free(passwd);
-                               passwd = NULL;
                                return APP2EXT_ERROR_SQLITE_REGISTRY;
                        }
                }
        }
 
        dev_node = _app2sd_do_loopback_duplicate_encryption_setup(pkgid,
-               temp_pkgid, temp_loopback_device, passwd, uid);
+                       temp_pkgid, temp_loopback_device, passwd, uid);
        if (!dev_node) {
                _E("losetup failed, device node is (%s)", dev_node);
                _app2sd_delete_loopback_device(temp_loopback_device);
                free(passwd);
-               passwd = NULL;
                return APP2EXT_ERROR_DO_LOSETUP;
        }
        free(passwd);
-       passwd = NULL;
        _D("duplicate setup SUCCESS");
 
        /* check whether loopback device is associated with
@@ -1509,6 +1387,7 @@ int _app2sd_duplicate_device(const char *pkgid,
                err_res = APP2EXT_ERROR_DO_LOSETUP;
                goto FINISH_OFF;
        }
+       free(devi);
        _D("losetup SUCCESS");
 #endif
 
@@ -1523,21 +1402,14 @@ int _app2sd_duplicate_device(const char *pkgid,
 
        /* do mounting for new dev*/
        ret = _app2sd_mount_app_content(temp_application_path, pkgid,
-               dev_node, MOUNT_TYPE_RW, dir_list,
-               APP2SD_PRE_UPGRADE, uid);
+                       dev_node, MOUNT_TYPE_RW, dir_list,
+                       APP2SD_PRE_UPGRADE, uid);
        if (ret) {
                _E("remount failed");
                err_res = APP2EXT_ERROR_MOUNT_PATH;
                goto FINISH_OFF;
        }
 
-#if !defined(TIZEN_FEATURE_APP2SD_DMCRYPT_ENCRYPTION)
-       if (devi) {
-               free(devi);
-               devi = NULL;
-       }
-#endif
-
        return APP2EXT_SUCCESS;
 
 FINISH_OFF:
@@ -1548,38 +1420,32 @@ FINISH_OFF:
                        _E("close dmcrypt device error(%d)", ret);
 #else
                result = _app2sd_detach_loop_device(dev_node);
-               if (result) {
+               if (result)
                        free(result);
-                       result = NULL;
-               }
 #endif
                _app2sd_delete_loopback_device(temp_loopback_device);
                free(dev_node);
-               dev_node = NULL;
        }
 
        return err_res;
 }
 
 int _app2sd_update_loopback_device_size(const char *pkgid,
-               const char *loopback_device,
-               const char *application_path,
-               const char *temp_pkgid,
-               const char *temp_loopback_device,
-               const char *temp_application_path,
-               int size, GList *dir_list,
+               const char *loopback_device, const char *application_path,
+               const char *temp_pkgid, const char *temp_loopback_device,
+               const char *temp_application_path, int size, GList *dir_list,
                uid_t uid)
 {
-       int ret = 0;
-       char *device_node = NULL;
-       char *old_device_node = NULL;
-       int err_res = 0;
-       char app_mmc_path[FILENAME_MAX] = { 0, };
-       char temp_app_mmc_path[FILENAME_MAX] = { 0, };
-
-       ret = _app2sd_duplicate_device(pkgid, loopback_device,
-               temp_pkgid, temp_application_path, temp_loopback_device,
-               dir_list, device_node, size, uid);
+       int ret;
+       char *old_device_node;
+       int err_res;
+       char app_mmc_path[FILENAME_MAX];
+       char temp_app_mmc_path[FILENAME_MAX];
+       int mount_type;
+
+       ret = _app2sd_duplicate_device(pkgid, loopback_device, temp_pkgid,
+                       temp_application_path, temp_loopback_device,
+                       dir_list, NULL, size, uid);
        if (ret) {
                _E("creating duplicate device failed");
                return ret;
@@ -1592,7 +1458,7 @@ int _app2sd_update_loopback_device_size(const char *pkgid,
 #else
        old_device_node = _app2sd_find_associated_device_node(loopback_device);
 #endif
-       if (NULL == old_device_node) {
+       if (old_device_node == NULL) {
                /* do loopback setup */
 #ifdef TIZEN_FEATURE_APP2SD_DMCRYPT_ENCRYPTION
                ret = _app2sd_dmcrypt_open_device(pkgid, loopback_device,
@@ -1611,32 +1477,27 @@ int _app2sd_update_loopback_device_size(const char *pkgid,
                        goto FINISH_OFF;
                }
 #endif
-               /* do mounting */
-               ret = _app2sd_mount_app_content(application_path, pkgid,
-                       old_device_node, MOUNT_TYPE_RW, dir_list,
-                       APP2SD_PRE_UPGRADE, uid);
-               if (ret) {
-                       _E("remount failed");
-                       err_res = APP2EXT_ERROR_MOUNT_PATH;
-               }
+               mount_type = MOUNT_TYPE_RW;
        } else {
-               /* do re-mounting */
-               ret = _app2sd_mount_app_content(application_path, pkgid,
-                       old_device_node, MOUNT_TYPE_RW_REMOUNT, dir_list,
+               mount_type = MOUNT_TYPE_RW_REMOUNT;
+       }
+
+       /* do mounting */
+       ret = _app2sd_mount_app_content(application_path, pkgid,
+                       old_device_node, mount_type, dir_list,
                        APP2SD_PRE_UPGRADE, uid);
-               if (ret) {
-                       _E("remount failed");
-                       err_res = APP2EXT_ERROR_MOUNT_PATH;
-               }
+       if (ret) {
+               _E("remount failed");
+               err_res = APP2EXT_ERROR_MOUNT_PATH;
        }
 
        snprintf(app_mmc_path, sizeof(app_mmc_path),
-                "%s/.mmc", application_path);
+                       "%s/.mmc", application_path);
        snprintf(temp_app_mmc_path, sizeof(temp_app_mmc_path),
-               "%s/.mmc", temp_application_path);
+                       "%s/.mmc", temp_application_path);
 
        ret = _app2sd_copy_ro_content(app_mmc_path,
-               temp_app_mmc_path, dir_list);
+                       temp_app_mmc_path, dir_list);
        if (ret) {
                _E("copy ro content failed");
                err_res = ret;
@@ -1708,10 +1569,8 @@ int _app2sd_update_loopback_device_size(const char *pkgid,
        return APP2EXT_SUCCESS;
 
 FINISH_OFF:
-       if (old_device_node) {
+       if (old_device_node)
                free(old_device_node);
-               old_device_node = NULL;
-       }
 
 #ifdef TIZEN_FEATURE_APP2SD_DMCRYPT_ENCRYPTION
        ret = _app2sd_dmcrypt_close_device(pkgid, uid);
@@ -1719,7 +1578,6 @@ FINISH_OFF:
                _E("close dmcrypt device error(%d)", ret);
 
        ret = _app2sd_dmcrypt_close_device(temp_pkgid, uid);
-
        if (ret)
                _E("close dmcrypt device error(%d)", ret);
 #else
@@ -1744,9 +1602,11 @@ FINISH_OFF:
 int _app2sd_force_clean(const char *pkgid, const char *application_path,
                const char *loopback_device, uid_t uid)
 {
-       int ret = APP2EXT_SUCCESS;
+       int ret;
 
-       /* unmount the loopback encrypted pseudo device from the application installation path */
+       /* unmount the loopback encrypted pseudo device from
+        * the application installation path
+        */
        ret = _app2sd_unmount_app_content(application_path);
        if (ret)
                _W("unable to unmount the app content (%d)", ret);
@@ -1759,13 +1619,15 @@ int _app2sd_force_clean(const char *pkgid, const char *application_path,
 #else
        ret = _app2sd_remove_all_loopback_encryption_setups(loopback_device);
        if (ret)
-               _W("unable to detach the loopback encryption setup for the application");
+               _W("unable to detach the loopback encryption setup for "
+                               "the application");
 #endif
 
        /* delete the loopback device from the SD card */
        ret = _app2sd_delete_loopback_device(loopback_device);
        if (ret)
-               _W("unable to detach the loopback encryption setup for the application");
+               _W("unable to detach the loopback encryption setup for "
+                               "the application");
 
        /* delete symlink */
        _app2sd_delete_symlink(application_path);
@@ -1779,6 +1641,5 @@ int _app2sd_force_clean(const char *pkgid, const char *application_path,
        if (ret)
                _W("cannot remove info from db");
 
-
        return ret;
 }
index 92195f4..ff2cceb 100644 (file)
@@ -200,4 +200,7 @@ char *_app2sd_find_associated_dmcrypt_device_node(const char *pkgid, uid_t uid);
 int _app2sd_check_is_luks_device(const char *device_path);
 #endif
 
+int _app2sd_get_loopback_device_path(const char *mmc_path, const char *pkgid,
+               uid_t uid, char *loopback_device, size_t len);
+
 #endif
index dc8e8e0..d5d7c62 100644 (file)
@@ -53,7 +53,8 @@ static int _app2sd_db_change_perm(const char *db_file)
        char *files[3];
        int ret, i;
        uid_t uid = APPFW_UID;
-       struct passwd userinfo, *result = NULL;
+       struct passwd userinfo;
+       struct passwd *result;
        files[0] = (char *)db_file;
        files[1] = journal_file;
        files[2] = NULL;
@@ -66,7 +67,8 @@ static int _app2sd_db_change_perm(const char *db_file)
                return -1;
        }
 
-       snprintf(journal_file, sizeof(journal_file), "%s%s", db_file, "-journal");
+       snprintf(journal_file, sizeof(journal_file), "%s%s", db_file,
+                       "-journal");
 
        ret = getpwuid_r(uid, &userinfo, pwuid_buf, sizeof(pwuid_buf), &result);
        if (ret != 0 || result == NULL) {
@@ -106,28 +108,27 @@ static int _app2sd_db_change_perm(const char *db_file)
        return 0;
 }
 
-int _app2sd_initialize_db()
+int _app2sd_initialize_db(void)
 {
        char *error_message = NULL;
        int ret;
-       FILE *fp = NULL;
+       FILE *fp;
 
        fp = fopen(APP2SD_DB_FILE, "r");
        if (fp != NULL) {
                fclose(fp);
                ret = db_util_open(APP2SD_DB_FILE, &app2sd_db,
-                       DB_UTIL_REGISTER_HOOK_METHOD);
+                               DB_UTIL_REGISTER_HOOK_METHOD);
 
                if (ret != SQLITE_OK) {
-                       _E("connect menu_db [%s] failed",
-                               APP2SD_DB_FILE);
+                       _E("connect menu_db [%s] failed", APP2SD_DB_FILE);
                        return -1;
                }
                return 0;
        }
 
        ret = db_util_open(APP2SD_DB_FILE, &app2sd_db,
-                DB_UTIL_REGISTER_HOOK_METHOD);
+                       DB_UTIL_REGISTER_HOOK_METHOD);
 
        if (ret != SQLITE_OK) {
                _E("connect menu_db [%s] failed",
@@ -135,12 +136,11 @@ int _app2sd_initialize_db()
                return -1;
        }
 
-       if (SQLITE_OK != sqlite3_exec(app2sd_db,
-               QUERY_CREATE_TABLE_APP2SD, NULL, NULL,
-               &error_message)) {
-               _E("don't execute query = (%s), " \
-                       "error message = (%s)",
-                       QUERY_CREATE_TABLE_APP2SD, error_message);
+       ret = sqlite3_exec(app2sd_db, QUERY_CREATE_TABLE_APP2SD, NULL, NULL,
+                       &error_message);
+       if (ret != SQLITE_OK) {
+               _E("don't execute query = (%s), error message = (%s)",
+                               QUERY_CREATE_TABLE_APP2SD, error_message);
                return -1;
        }
 
@@ -154,13 +154,13 @@ int _app2sd_initialize_db()
 
 static int _app2sd_check_existing_info(const char *pkgid, uid_t uid)
 {
-       char *query = NULL;
-       const char *val = NULL;
-       sqlite3_stmt *stmt = NULL;
+       char *query;
+       const char *val;
+       sqlite3_stmt *stmt;
        int ret = 0;
 
-       query = sqlite3_mprintf("select count(*) from app2sd_info " \
-               "where pkgid=%Q and uid=%d", pkgid, uid);
+       query = sqlite3_mprintf("select count(*) from app2sd_info "
+                       "where pkgid=%Q and uid=%d", pkgid, uid);
        if (query == NULL) {
                _E("failed to make a query");
                return -1;
@@ -190,8 +190,8 @@ int _app2sd_set_info_in_db(const char *pkgid, const char *passwd,
                const char *loopback_device, uid_t uid)
 {
        char *error_message = NULL;
-       char *query = NULL;
-       int ret = 0;
+       char *query;
+       int ret;
 
        ret = _app2sd_check_existing_info(pkgid, uid);
        if (ret < 0) {
@@ -200,18 +200,20 @@ int _app2sd_set_info_in_db(const char *pkgid, const char *passwd,
        }
 
        if (ret == 0)
-               query = sqlite3_mprintf("insert into app2sd_info " \
-                       "(pkgid, password, filename, uid) values (%Q, %Q, %Q, %d)",
-                       pkgid, passwd, loopback_device, uid);
+               query = sqlite3_mprintf("insert into app2sd_info "
+                               "(pkgid, password, filename, uid) "
+                               "values (%Q, %Q, %Q, %d)",
+                               pkgid, passwd, loopback_device, uid);
        else
-               query = sqlite3_mprintf("update app2sd_info " \
-                       "set password=%Q, filename=%Q where pkgid=%Q and uid=%d",
-                       passwd, loopback_device, pkgid, uid);
+               query = sqlite3_mprintf("update app2sd_info "
+                               "set password=%Q, filename=%Q "
+                               "where pkgid=%Q and uid=%d",
+                               passwd, loopback_device, pkgid, uid);
 
        ret = sqlite3_exec(app2sd_db, query, NULL, NULL, &error_message);
        if (ret != SQLITE_OK) {
                _E("failed to execute query(%s), error message(%s)",
-                       query, error_message);
+                               query, error_message);
                sqlite3_free(query);
                return APP2EXT_ERROR_SQLITE_REGISTRY;
        }
@@ -223,7 +225,7 @@ int _app2sd_set_info_in_db(const char *pkgid, const char *passwd,
 int _app2sd_remove_info_from_db(const char *pkgid, uid_t uid)
 {
        char *error_message = NULL;
-       char *query = NULL;
+       char *query;
        int ret = 0;
 
        query = sqlite3_mprintf("delete from app2sd_info " \
index 3867a44..aa3abb6 100644 (file)
 ########### Internal APIs ##################
  */
 
-/*Note: Don't use any printf statement inside this function*/
-/*This function is similar to Linux's "system()"  for executing a process.*/
+/* Note: Don't use any printf statement inside this function
+ * This function is similar to Linux's "system()" for executing a process.
+ */
 int _xsystem(const char *argv[])
 {
-       int status = 0;
+       int status;
        pid_t pid;
-       char err_buf[1024] = {0,};
+       char err_buf[1024];
 
        pid = fork();
        switch (pid) {
@@ -52,7 +53,7 @@ int _xsystem(const char *argv[])
                strerror_r(errno, err_buf, sizeof(err_buf));
                if (execvp(argv[0], (char *const *)argv) < 0)
                        fprintf(stderr, "execvp failed %d....%s\n",
-                               errno, err_buf);
+                                       errno, err_buf);
                _exit(-1);
        default:
                /* parent */
@@ -74,35 +75,25 @@ int _xsystem(const char *argv[])
        return WEXITSTATUS(status);
 }
 
-
 /*
  * This function checks and returns MMC status
  */
 int _app2sd_check_mmc_status(char **sdpath)
 {
-       int ret = 0;
-       int storage_id = 0;
-       char *sd_mount_path = NULL;
+       int ret;
+       int storage_id;
+       char *sd_mount_path;
 
        ret = storage_get_primary_sdcard(&storage_id, &sd_mount_path);
        if (ret != STORAGE_ERROR_NONE) {
                _E("failed to get primary sdcard (%d)", ret);
-               if (sd_mount_path)
-                       free(sd_mount_path);
                return APP2EXT_ERROR_MMC_STATUS;
        }
-       if (sd_mount_path) {
-               _D("primary sdcard: id(%d), mount_path(%s)",
-                       storage_id, sd_mount_path);
-               *sdpath = sd_mount_path;
-               return APP2EXT_SUCCESS;
-       }
 
-       _E("there is no primary sdcard");
-       if (sd_mount_path)
-               free(sd_mount_path);
+       _D("primary sdcard: id(%d), mount_path(%s)", storage_id, sd_mount_path);
+       *sdpath = sd_mount_path;
 
-       return APP2EXT_ERROR_MMC_STATUS;
+       return APP2EXT_SUCCESS;
 }
 
 /*
@@ -132,7 +123,7 @@ int _app2sd_get_available_free_memory(char *mmc_path, int *free_mem)
                return APP2EXT_ERROR_MMC_INFORMATION;
        }
 
-       temp = (unsigned long long)buf.f_bsize*buf.f_bavail;
+       temp = (unsigned long long)buf.f_bsize * buf.f_bavail;
        *free_mem = (int)(temp/(1024*1024));
 
        return 0;
@@ -140,79 +131,81 @@ int _app2sd_get_available_free_memory(char *mmc_path, int *free_mem)
 
 void _app2sd_delete_symlink(const char *dirname)
 {
-       int ret = 0;
-       DIR *dp = NULL;
-       struct dirent *ep = NULL;
-       char abs_filename[FILENAME_MAX] = { 0, };
+       int ret;
+       DIR *dp;
+       struct dirent *ep;
+       char abs_filename[FILENAME_MAX];
+       char mmc_path[PATH_MAX];
+       char *path;
 
        dp = opendir(dirname);
-       if (dp != NULL) {
-               while ((ep = readdir(dp)) != NULL) {
-                       char mmc_path[PATH_MAX] = {0};
-
-                       if (!strcmp(ep->d_name, ".") || !strcmp(ep->d_name, ".."))
-                               continue;
-
-                       /*get realpath find symlink to ".mmc" and unlink it*/
-                       snprintf(abs_filename, sizeof(abs_filename), "%s/%s", dirname, ep->d_name);
-                       char *path = realpath(abs_filename, mmc_path);
-                       if (!path)
-                               _E("realpath failed");
-
-                       if (strstr(mmc_path, ".mmc")) {
-                               _E("force unlink [%s]", abs_filename);
-                               if (unlink(abs_filename)) {
-                                       if (errno == ENOENT)
-                                               _W("Unable to access file %s", abs_filename);
-                                       else
-                                               _E("Unable to delete %s", abs_filename);
-                               }
-                       }
-
+       if (dp == NULL)
+               return;
+
+       while ((ep = readdir(dp)) != NULL) {
+               if (!strcmp(ep->d_name, ".") || !strcmp(ep->d_name, ".."))
+                       continue;
+
+               /* get realpath find symlink to ".mmc" and unlink it */
+               snprintf(abs_filename, sizeof(abs_filename), "%s/%s", dirname,
+                               ep->d_name);
+               path = realpath(abs_filename, mmc_path);
+               if (!path)
+                       _E("realpath failed");
+
+               if (strstr(mmc_path, ".mmc") == NULL)
+                       continue;
+
+               _E("force unlink [%s]", abs_filename);
+               if (unlink(abs_filename)) {
+                       if (errno == ENOENT)
+                               _W("Unable to access file %s", abs_filename);
+                       else
+                               _E("Unable to delete %s", abs_filename);
                }
-               (void)closedir(dp);
-
-               /*delete ".mmc" folder*/
-               snprintf(abs_filename, sizeof(abs_filename), "%s/.mmc", dirname);
-               ret = remove(abs_filename);
-               if (ret == -1)
-                       return;
+       }
+       (void)closedir(dp);
+
+       /* delete ".mmc" directory */
+       snprintf(abs_filename, sizeof(abs_filename), "%s/.mmc", dirname);
+       ret = remove(abs_filename);
+       if (ret == -1) {
+               _W("failed to remove %s", abs_filename);
+               return;
        }
 }
 
 int _app2sd_copy_dir(const char *src, const char *dest)
 {
-       int ret = APP2EXT_SUCCESS;
-       DIR *dir = NULL;
+       int ret;
+       DIR *dir;
        const char *argv_bin[] = { "/bin/cp", "-raf", src, dest, NULL };
 
        /* check existence  before copy */
        dir = opendir(src);
-       if (dir) {
-               closedir(dir);
-       } else {
+       if (dir == NULL) {
                if (errno == ENOENT) {
                        _W("src(%s) not exist, skip!", src);
-                       return ret;
+                       return APP2EXT_SUCCESS;
                } else {
                        _E("failed to open src(%s) dir, errno(%d)", src, errno);
                        return APP2EXT_ERROR_ACCESS_FILE;
                }
        }
+       closedir(dir);
 
        dir = opendir(dest);
-       if (dir) {
-               closedir(dir);
-       } else {
+       if (dir == NULL) {
                if (errno == ENOENT) {
                        _E("dest(%s) not exist, failed!", dest);
                        return APP2EXT_ERROR_ACCESS_FILE;
                } else {
-                       _E("failed to open dest(%s) dir, errno(%d)",
+                       _E("failed to open dest(%s) dir: errno(%d)",
                                        dest, errno);
                        return APP2EXT_ERROR_ACCESS_FILE;
                }
        }
+       closedir(dir);
 
        ret = _xsystem(argv_bin);
        if (ret) {
@@ -224,78 +217,73 @@ int _app2sd_copy_dir(const char *src, const char *dest)
 
 int _app2sd_rename_dir(const char *old_name, const char *new_name)
 {
-       int ret = APP2EXT_SUCCESS;
+       int ret;
        const char *argv_bin[] = { "/bin/mv", old_name, new_name, NULL };
        ret = _xsystem(argv_bin);
        if (ret) {
                _E("mv/rename fail");
                return APP2EXT_ERROR_ACCESS_FILE;
        }
-       return ret;
+       return APP2EXT_SUCCESS;
 }
 
 unsigned long long _app2sd_calculate_dir_size(char *dirname)
 {
-       static unsigned long long total = 0;
+       unsigned long long total = 0;
        DIR *dp = NULL;
        struct dirent *ep = NULL;
        char abs_filename[FILENAME_MAX] = { 0, };;
+       struct stat sb;
 
        dp = opendir(dirname);
-       if (dp != NULL) {
-               while ((ep = readdir(dp)) != NULL) {
-                       struct stat stFileInfo;
+       if (dp == NULL) {
+               _E("error in opening directory");
+               return 0;
+       }
 
-                       snprintf(abs_filename, sizeof(abs_filename), "%s/%s", dirname,
+       while ((ep = readdir(dp)) != NULL) {
+               snprintf(abs_filename, sizeof(abs_filename), "%s/%s", dirname,
                                 ep->d_name);
-
-                       if (stat(abs_filename, &stFileInfo) < 0)
-                               perror(abs_filename);
-                       else {
-                               total += stFileInfo.st_size;
-
-                               if (S_ISDIR(stFileInfo.st_mode)) {
-                                       if (strcmp(ep->d_name, ".")
-                                           && strcmp(ep->d_name, "..")) {
-                                               _app2sd_calculate_dir_size
-                                                   (abs_filename);
-                                       }
-                               } else {
-                                       /*Do Nothing */
-                               }
-                       }
+               if (stat(abs_filename, &sb) < 0) {
+                       perror(abs_filename);
+                       continue;
                }
-               (void)closedir(dp);
-       } else {
-               _E("error in opening directory");
+               total += sb.st_size;
+               if (!S_ISDIR(sb.st_mode))
+                       continue;
+
+               if (!strcmp(ep->d_name, ".") || !strcmp(ep->d_name, ".."))
+                       continue;
+               /* calculate subdirectory */
+               total += _app2sd_calculate_dir_size(abs_filename);
        }
+       (void)closedir(dp);
+
        return total;
 }
 
 unsigned long long _app2sd_calculate_file_size(const char *filename)
 {
-       struct stat stFileInfo;
+       struct stat sb;
        _D("calculating file size for (%s)", filename);
 
-       if (stat(filename, &stFileInfo) < 0) {
+       if (stat(filename, &sb) < 0) {
                perror(filename);
                return 0;
-       } else
-               return stFileInfo.st_size;
+       }
+
+       return sb.st_size;
 }
 
-/*Note: Don't use any printf statement inside this function*/
-char *_app2sd_encrypt_device(const char *device,
-       const char *loopback_device, char *passwd)
+/* Note: Don't use any printf statement inside this function */
+char *_app2sd_execute_command(const char *argv[])
 {
-       const char *argv[] = { "/sbin/losetup", device,
-               loopback_device, NULL };
-       pid_t pid = 0;
-       int my_pipe[2] = { 0, };
+       pid_t pid;
+       int my_pipe[2];
        char buf[FILENAME_MAX] = { 0, };
-       char *ret_result = NULL;
-       int result = 0;
-       char err_buf[1024] = { 0,};
+       char *ret_result;
+       int result;
+       char err_buf[1024];
 
        if (pipe(my_pipe) < 0) {
                fprintf(stderr, "Unable to create pipe\n");
@@ -308,26 +296,23 @@ char *_app2sd_encrypt_device(const char *device,
                return NULL;
        case 0:
                /* child */
-               close(1);
-               close(2);
-               result = dup(my_pipe[1]);
+               close(0);
+               result = dup2(my_pipe[1], 1);
                if (result < 0) {
                        strerror_r(errno, err_buf, sizeof(err_buf));
-                       fprintf(stderr, "dup failed %d....%s\n",
-                               errno, err_buf);
+                       fprintf(stderr, "dup failed %d...%s\n", errno, err_buf);
                        _exit(-1);
                }
-               result = dup(my_pipe[1]);
+               result = dup2(my_pipe[1], 2);
                if (result < 0) {
                        strerror_r(errno, err_buf, sizeof(err_buf));
-                       fprintf(stderr, "dup failed %d....%s\n",
-                               errno, err_buf);
+                       fprintf(stderr, "dup failed %d...%s\n", errno, err_buf);
                        _exit(-1);
                }
                if (execvp(argv[0], (char *const *)argv) < 0) {
                        strerror_r(errno, err_buf, sizeof(err_buf));
                        fprintf(stderr, "execvp failed %d....%s\n",
-                               errno, err_buf); /*Don't use d_msg_app2sd */
+                                       errno, err_buf);
                }
                _exit(-1);
        default:
@@ -336,154 +321,48 @@ char *_app2sd_encrypt_device(const char *device,
                result = read(my_pipe[0], buf, FILENAME_MAX);
                if (result < 0) {
                        strerror_r(errno, err_buf, sizeof(err_buf));
-                       fprintf(stderr, "read failed %d....%s\n",
-                               errno, err_buf);
+                       fprintf(stderr, "read failed %d..%s\n", errno, err_buf);
                }
                break;
        }
 
-       ret_result = (char *)malloc(strlen(buf) + 1);
+       ret_result = strdup(buf);
        if (ret_result == NULL) {
-               _E("malloc failed");
+               _E("strdup failed");
                return NULL;
        }
-       memset(ret_result, '\0', strlen(buf) + 1);
-       memcpy(ret_result, buf, strlen(buf));
 
        return ret_result;
+
 }
 
-/*Note: Don't use any printf statement inside this function*/
+/* Note: Don't use any printf statement inside this function */
+char *_app2sd_encrypt_device(const char *device, const char *loopback_device,
+               char *passwd)
+{
+       const char *argv[] = { "/sbin/losetup", device, loopback_device, NULL };
+       return _app2sd_execute_command(argv);
+}
+
+/* Note: Don't use any printf statement inside this function */
 char *_app2sd_detach_loop_device(const char *device)
 {
        const char *argv[] = { "/sbin/losetup", "-d", device, NULL };
-       pid_t pid;
-       int my_pipe[2] = { 0, };
-       char buf[FILENAME_MAX] = { 0, };
-       char *ret_result = NULL;
-       int result = 0;
-       char err_buf[1024] = {0,};
-
-       if (pipe(my_pipe) < 0) {
-               fprintf(stderr, "Unable to create pipe\n");
-               return NULL;
-       }
-       pid = fork();
-       switch (pid) {
-       case -1:
-               perror("fork failed");
-               return NULL;
-       case 0:
-               /* child */
-               close(1);
-               close(2);
-               result = dup(my_pipe[1]);
-               if (result < 0) {
-                       strerror_r(errno, err_buf, sizeof(err_buf));
-                       fprintf(stderr, "dup failed %d....%s\n",
-                               errno, err_buf);
-                       _exit(-1);
-               }
-               result = dup(my_pipe[1]);
-               if (result < 0) {
-                       strerror_r(errno, err_buf, sizeof(err_buf));
-                       fprintf(stderr, "dup failed %d....%s\n",
-                               errno, err_buf);
-                       _exit(-1);
-               }
-               if (execvp(argv[0], (char *const *)argv) < 0) {
-                       fprintf(stderr, "execvp failed\n");
-                               /* Don't use d_msg_app2sd */
-               }
-               _exit(-1);
-       default:
-               /* parent */
-               close(my_pipe[1]);
-               result = read(my_pipe[0], buf, FILENAME_MAX);
-               if (result < 0) {
-                       strerror_r(errno, err_buf, sizeof(err_buf));
-                       fprintf(stderr, "read failed %d....%s\n",
-                               errno, err_buf);
-               }
-               break;
-       }
-
-       ret_result = (char *)malloc(strlen(buf) + 1);
-       if (ret_result == NULL) {
-               _E("malloc failed");
-               return NULL;
-       }
-       memset(ret_result, '\0', strlen(buf) + 1);
-       memcpy(ret_result, buf, strlen(buf));
-
-       return ret_result;
+       return _app2sd_execute_command(argv);
 }
 
 /* Note: Don't use any printf statement inside this function*/
 char *_app2sd_find_associated_device(const char *loopback_device)
 {
        const char *argv[] = { "/sbin/losetup", "-a", NULL };
-       pid_t pid;
-       int my_pipe[2] = { 0, };
-       char buf[FILENAME_MAX] = { 0, };
-       char *ret_result_temp = NULL;
+       char *ret_result_temp;
        char *ret_result = NULL;
-       char *line = NULL;;
-       char *save_str = NULL;
-       int result = 0;
-       char err_buf[1024] = {0,};
-
-       if (pipe(my_pipe) < 0) {
-               fprintf(stderr, "Unable to create pipe\n");
-               return NULL;
-       }
-       pid = fork();
-       switch (pid) {
-       case -1:
-               perror("fork failed");
-               return NULL;
-       case 0:
-               /* child */
-               close(1);
-               close(2);
-               result = dup(my_pipe[1]);
-               if (result < 0) {
-                       strerror_r(errno, err_buf, sizeof(err_buf));
-                       fprintf(stderr, "dup failed %d....%s\n",
-                               errno, err_buf);
-                       _exit(-1);
-               }
-               result = dup(my_pipe[1]);
-               if (result < 0) {
-                       strerror_r(errno, err_buf, sizeof(err_buf));
-                       fprintf(stderr, "dup failed %d....%s\n",
-                               errno, err_buf);
-                       _exit(-1);
-               }
-               if (execvp(argv[0], (char *const *)argv) < 0) {
-                       fprintf(stderr, "execvp failed\n");
-                               /* Don't use d_msg_app2sd */
-               }
-               _exit(-1);
-       default:
-               /* parent */
-               close(my_pipe[1]);
-               result = read(my_pipe[0], buf, FILENAME_MAX);
-               if (result < 0) {
-                       strerror_r(errno, err_buf, sizeof(err_buf));
-                       fprintf(stderr, "read failed %d....%s\n",
-                               errno, err_buf);
-               }
-               break;
-       }
+       char *line;
+       char *save_str;
 
-       ret_result_temp = (char *)malloc(strlen(buf) + 1);
-       if (ret_result_temp == NULL) {
-               _E("malloc failed");
+       ret_result_temp = _app2sd_execute_command(argv);
+       if (ret_result_temp == NULL)
                return NULL;
-       }
-       memset(ret_result_temp, '\0', strlen(buf) + 1);
-       memcpy(ret_result_temp, buf, strlen(buf));
 
        line = strtok_r(ret_result_temp, "\n", &save_str);
        while (line) {
@@ -505,66 +384,7 @@ char *_app2sd_find_associated_device(const char *loopback_device)
 char *_app2sd_find_free_device(void)
 {
        const char *argv[] = { "/sbin/losetup", "-f", NULL };
-       pid_t pid;
-       int my_pipe[2] = { 0, };
-       char buf[FILENAME_MAX + 1] = { 0, };
-       char *ret_result = NULL;
-       int result = 0;
-       char err_buf[1024] = {0,};
-
-       if (pipe(my_pipe) < 0) {
-               fprintf(stderr, "Unable to create pipe\n");
-               return NULL;
-       }
-       pid = fork();
-       switch (pid) {
-       case -1:
-               perror("fork failed");
-               return NULL;
-       case 0:
-               /* child */
-               close(1);
-               close(2);
-               result = dup(my_pipe[1]);
-               if (result < 0) {
-                       strerror_r(errno, err_buf, sizeof(err_buf));
-                       fprintf(stderr, "dup failed %d....%s\n",
-                               errno, err_buf);
-                       _exit(-1);
-               }
-               result = dup(my_pipe[1]);
-               if (result < 0) {
-                       strerror_r(errno, err_buf, sizeof(err_buf));
-                       fprintf(stderr, "dup failed %d....%s\n",
-                               errno, err_buf);
-                       _exit(-1);
-               }
-               if (execvp(argv[0], (char *const *)argv) < 0) {
-                       fprintf(stderr, "execvp failed\n");
-                               /* Don't use d_msg_app2sd */
-               }
-               _exit(-1);
-       default:
-               /* parent */
-               close(my_pipe[1]);
-               result = read(my_pipe[0], buf, FILENAME_MAX);
-               if (result < 0) {
-                       strerror_r(errno, err_buf, sizeof(err_buf));
-                       fprintf(stderr, "read failed %d....%s\n",
-                               errno, err_buf);
-               }
-               break;
-       }
-
-       ret_result = (char *)malloc(strlen(buf) + 1);
-       if (ret_result == NULL) {
-               _E("malloc failed");
-               return NULL;
-       }
-       memset(ret_result, '\0', strlen(buf) + 1);
-       memcpy(ret_result, buf, strlen(buf));
-
-       return ret_result;
+       return _app2sd_execute_command(argv);
 }
 
 /*
@@ -610,3 +430,18 @@ int _app2sd_check_is_luks_device(const char *device_path)
        return result;
 }
 #endif
+
+int _app2sd_get_loopback_device_path(const char *mmc_path,
+               const char *pkgid, uid_t uid, char *loopback_device, size_t len)
+{
+       char *encoded_id;
+
+       encoded_id = _app2sd_get_encoded_name(pkgid, uid);
+       if (encoded_id == NULL)
+               return APP2EXT_ERROR_MEMORY_ALLOC_FAILED;
+
+       snprintf(loopback_device, len, "%s/%s/%s", mmc_path, EXTIMG_DIR,
+                       encoded_id);
+       free(encoded_id);
+       return APP2EXT_SUCCESS;
+}
index 3be7117..d9e0914 100644 (file)
@@ -52,43 +52,36 @@ gboolean __exit_app2sd_server(void *data)
 static int __app2sd_get_sender_unixinfo(GDBusConnection *conn,
                const char *sender_name, const char *type)
 {
-       GDBusMessage *msg = NULL;
-       GDBusMessage *reply = NULL;
+       GDBusMessage *msg;
+       GDBusMessage *reply;
        GError *err = NULL;
        GVariant *body;
-       int ret = -1;
        unsigned int value;
 
        msg = g_dbus_message_new_method_call("org.freedesktop.DBus",
-               "/org/freedesktop/DBus", "org.freedesktop.DBus", type);
+                       "/org/freedesktop/DBus", "org.freedesktop.DBus", type);
        if (!msg) {
                _E("Can't allocate new method call");
-               goto out;
+               return -1;
        }
 
        g_dbus_message_set_body(msg, g_variant_new("(s)", sender_name));
        reply = g_dbus_connection_send_message_with_reply_sync(conn, msg,
-               G_DBUS_SEND_MESSAGE_FLAGS_NONE, -1, NULL, NULL, &err);
-
+                       G_DBUS_SEND_MESSAGE_FLAGS_NONE, -1, NULL, NULL, &err);
+       g_object_unref(msg);
        if (!reply) {
                if (err != NULL) {
                        _E("Failed to get info [%s]", err->message);
                        g_error_free(err);
                }
-               goto out;
+               return -1;
        }
 
        body = g_dbus_message_get_body(reply);
        g_variant_get(body, "(u)", &value);
-       ret = (int)value;
-
-out:
-       if (msg)
-               g_object_unref(msg);
-       if (reply)
-               g_object_unref(reply);
+       g_object_unref(reply);
 
-       return ret;
+       return (int)value;
 }
 
 /*
@@ -116,7 +109,7 @@ static int __app2sd_get_sender_uid(GDBusConnection *conn,
        int uid = -1;
 
        uid = __app2sd_get_sender_unixinfo(conn, sender_name,
-               "GetConnectionUnixUser");
+                       "GetConnectionUnixUser");
        if (uid < 0)
                _E("failed to get uid");
 
@@ -217,7 +210,8 @@ static const gchar introspection_xml[] =
 
 static bool _app2sd_server_check_permission(uid_t sender_uid, uid_t target_uid)
 {
-       if (sender_uid != 0 && sender_uid != APPFW_UID && sender_uid != target_uid) {
+       if (sender_uid != 0 && sender_uid != APPFW_UID &&
+                       sender_uid != target_uid) {
                _E("Not permitted user!, uid(%d)", sender_uid);
                return false;
        }
@@ -225,104 +219,110 @@ static bool _app2sd_server_check_permission(uid_t sender_uid, uid_t target_uid)
        return true;
 }
 
-static void _app2sd_server_return_method_error(GDBusMethodInvocation *invocation, int result)
+static void _app2sd_server_return_method_error(
+               GDBusMethodInvocation *invocation, int result)
 {
-       GVariant *param = NULL;
+       GVariant *param;
 
        param = g_variant_new("(i)", result);
        g_dbus_method_invocation_return_value(invocation, param);
 }
 
-static int _app2sd_server_generate_directory_list(GVariantIter *iter, GList **list)
+static int _app2sd_server_generate_directory_list(GVariantIter *iter,
+               GList **list)
 {
        int result = APP2EXT_SUCCESS;
        gchar *str = NULL;
        int type;
 
-       app2ext_dir_details *dir_detail = NULL;
+       app2ext_dir_details *dir_detail;
        while (g_variant_iter_loop(iter, "(si)", &str, &type)) {
-               if (str) {
-                       _D("str(%s), type(%d)", str, type);
-
-                       /* generate dir_list */
-                       dir_detail = (app2ext_dir_details *)calloc(1, sizeof(app2ext_dir_details));
-                       if (dir_detail == NULL) {
-                               _E("memory allocation failed");
-                               result = APP2EXT_ERROR_MEMORY_ALLOC_FAILED;
-                               break;
-                       }
-
-                       dir_detail->name = strdup((char *)str);
-                       if (dir_detail->name == NULL) {
-                               _E("out of memory");
-                               free(dir_detail);
-                               result = APP2EXT_ERROR_MEMORY_ALLOC_FAILED;
-                               break;
-                       }
-
-                       dir_detail->type = type;
-                       *list = g_list_append(*list, dir_detail);
+               if (!str)
+                       continue;
+               _D("str(%s), type(%d)", str, type);
+
+               /* generate dir_list */
+               dir_detail = (app2ext_dir_details *)calloc(
+                               1, sizeof(app2ext_dir_details));
+               if (dir_detail == NULL) {
+                       _E("memory allocation failed");
+                       result = APP2EXT_ERROR_MEMORY_ALLOC_FAILED;
+                       break;
                }
+               dir_detail->name = strdup((char *)str);
+               if (dir_detail->name == NULL) {
+                       _E("out of memory");
+                       free(dir_detail);
+                       result = APP2EXT_ERROR_MEMORY_ALLOC_FAILED;
+                       break;
+               }
+               dir_detail->type = type;
+               *list = g_list_append(*list, dir_detail);
        }
 
        return result;
 }
 
-static void _app2sd_server_pre_app_install(GDBusConnection *connection, const gchar *sender,
-       GVariant *parameters, GDBusMethodInvocation *invocation, uid_t sender_uid)
+static void _app2sd_server_pre_app_install(GDBusConnection *connection,
+               const gchar *sender, GVariant *parameters,
+               GDBusMethodInvocation *invocation, uid_t sender_uid)
 {
-       GVariant *param = NULL;
-       int result = APP2EXT_SUCCESS;
+       GVariant *param;
        int size;
        char *pkgid = NULL;
        GVariantIter *iter;
-       int ret = 0;
+       int ret;
        uid_t target_uid = -1;
-       GList *dir_list = NULL;
+       GList *dir_list;
        GList *list = NULL;
 
-       g_variant_get(parameters, "(&sia(si)i)", &pkgid, &size, &iter, &target_uid);
+       g_variant_get(parameters, "(&sia(si)i)", &pkgid, &size, &iter,
+                       &target_uid);
 
        _D("pkgid(%s), size(%d),sender_uid(%d), target_uid(%d)",
-               pkgid, size, sender_uid, target_uid);
+                       pkgid, size, sender_uid, target_uid);
 
        if (!_app2sd_server_check_permission(sender_uid, target_uid)) {
                _E("Not permitted user!");
                g_variant_iter_free(iter);
                _app2sd_server_return_method_error(invocation,
-                       APP2EXT_ERROR_OPERATION_NOT_PERMITTED);
+                               APP2EXT_ERROR_OPERATION_NOT_PERMITTED);
                return;
        }
 
-       result = _app2sd_server_generate_directory_list(iter, &list);
+       ret = _app2sd_server_generate_directory_list(iter, &list);
        g_variant_iter_free(iter);
+       if (ret) {
+               _E("error(%d)", ret);
+               _app2sd_server_return_method_error(invocation, ret);
+               return;
+       }
 
        dir_list = g_list_first(list);
        ret = app2sd_usr_pre_app_install(pkgid, dir_list, size, target_uid);
-       if (ret) {
+       if (ret)
                _E("error(%d)", ret);
-               result = ret;
-       }
        g_list_free_full(dir_list, __free_dir_detail);
 
-       param = g_variant_new("(i)", result);
+       param = g_variant_new("(i)", ret);
        g_dbus_method_invocation_return_value(invocation, param);
 }
 
-static void _app2sd_server_post_app_install(GDBusConnection *connection, const gchar *sender,
-       GVariant *parameters, GDBusMethodInvocation *invocation, uid_t sender_uid)
+static void _app2sd_server_post_app_install(GDBusConnection *connection,
+               const gchar *sender, GVariant *parameters,
+               GDBusMethodInvocation *invocation, uid_t sender_uid)
 {
-       GVariant *param = NULL;
-       int result = APP2EXT_SUCCESS;
+       GVariant *param;
        char *pkgid = NULL;
        int install_status = 0;
        int target_uid = -1;
-       int ret = 0;
+       int ret;
 
-       g_variant_get(parameters, "(&sii)", &pkgid, &install_status, &target_uid);
+       g_variant_get(parameters, "(&sii)", &pkgid, &install_status,
+                       &target_uid);
 
        _D("pkgid(%s), install_status(%d), sender_uid(%d), target_uid(%d)",
-               pkgid, install_status, sender_uid, target_uid);
+                       pkgid, install_status, sender_uid, target_uid);
 
        if (!_app2sd_server_check_permission(sender_uid, target_uid)) {
                _E("Not permitted user!");
@@ -332,32 +332,31 @@ static void _app2sd_server_post_app_install(GDBusConnection *connection, const g
        }
 
        ret = app2sd_usr_post_app_install(pkgid, install_status, target_uid);
-       if (ret) {
+       if (ret)
                _E("error(%d)", ret);
-               result = ret;
-       }
 
-       param = g_variant_new("(i)", result);
+       param = g_variant_new("(i)", ret);
        g_dbus_method_invocation_return_value(invocation, param);
 }
 
-static void _app2sd_server_pre_app_upgrade(GDBusConnection *connection, const gchar *sender,
-       GVariant *parameters, GDBusMethodInvocation *invocation, uid_t sender_uid)
+static void _app2sd_server_pre_app_upgrade(GDBusConnection *connection,
+               const gchar *sender, GVariant *parameters,
+               GDBusMethodInvocation *invocation, uid_t sender_uid)
 {
-       GVariant *param = NULL;
-       int result = APP2EXT_SUCCESS;
+       GVariant *param;
        int size;
        char *pkgid = NULL;
        GVariantIter *iter;
        uid_t target_uid = -1;
-       int ret = 0;
-       GList *dir_list = NULL;
+       int ret;
+       GList *dir_list;
        GList *list = NULL;
 
-       g_variant_get(parameters, "(&sia(si)i)", &pkgid, &size, &iter, &target_uid);
+       g_variant_get(parameters, "(&sia(si)i)", &pkgid, &size, &iter,
+                       &target_uid);
 
        _D("pkgid(%s), size(%d), sender_uid(%d), target_uid(%d)",
-               pkgid, size, sender_uid, target_uid);
+                       pkgid, size, sender_uid, target_uid);
 
        if (!_app2sd_server_check_permission(sender_uid, target_uid)) {
                _E("Not permitted user!");
@@ -367,225 +366,221 @@ static void _app2sd_server_pre_app_upgrade(GDBusConnection *connection, const gc
                return;
        }
 
-       result = _app2sd_server_generate_directory_list(iter, &list);
+       ret = _app2sd_server_generate_directory_list(iter, &list);
        g_variant_iter_free(iter);
+       if (ret) {
+               _E("error(%d)", ret);
+               _app2sd_server_return_method_error(invocation, ret);
+               return;
+       }
 
        dir_list = g_list_first(list);
        ret = app2sd_usr_pre_app_upgrade(pkgid, dir_list, size, target_uid);
-       if (ret) {
+       if (ret)
                _E("error(%d)", ret);
-               result = ret;
-       }
        g_list_free_full(dir_list, __free_dir_detail);
 
-       param = g_variant_new("(i)", result);
+       param = g_variant_new("(i)", ret);
        g_dbus_method_invocation_return_value(invocation, param);
 }
 
-static void _app2sd_server_post_app_upgrade(GDBusConnection *connection, const gchar *sender,
-       GVariant *parameters, GDBusMethodInvocation *invocation, uid_t sender_uid)
+static void _app2sd_server_post_app_upgrade(GDBusConnection *connection,
+               const gchar *sender, GVariant *parameters,
+               GDBusMethodInvocation *invocation, uid_t sender_uid)
 {
-       GVariant *param = NULL;
-       int result = APP2EXT_SUCCESS;
+       GVariant *param;
        char *pkgid = NULL;
        int install_status = 0;
        uid_t target_uid = -1;
-       int ret = 0;
+       int ret;
 
-       g_variant_get(parameters, "(&sii)", &pkgid, &install_status, &target_uid);
+       g_variant_get(parameters, "(&sii)", &pkgid, &install_status,
+                       &target_uid);
 
        _D("pkgid(%s), install_status(%d), sender_uid(%d), target_uid(%d)",
-               pkgid, install_status, sender_uid, target_uid);
+                       pkgid, install_status, sender_uid, target_uid);
 
        if (!_app2sd_server_check_permission(sender_uid, target_uid)) {
                _E("Not permitted user!");
                _app2sd_server_return_method_error(invocation,
-                       APP2EXT_ERROR_OPERATION_NOT_PERMITTED);
+                               APP2EXT_ERROR_OPERATION_NOT_PERMITTED);
                return;
        }
 
        ret = app2sd_usr_post_app_upgrade(pkgid, install_status, target_uid);
-       if (ret) {
+       if (ret)
                _E("error(%d)", ret);
-               result = ret;
-       }
 
-       param = g_variant_new("(i)", result);
+       param = g_variant_new("(i)", ret);
        g_dbus_method_invocation_return_value(invocation, param);
 }
 
-static void _app2sd_server_pre_app_uninstall(GDBusConnection *connection, const gchar *sender,
-       GVariant *parameters, GDBusMethodInvocation *invocation, uid_t sender_uid)
+static void _app2sd_server_pre_app_uninstall(GDBusConnection *connection,
+               const gchar *sender, GVariant *parameters,
+               GDBusMethodInvocation *invocation, uid_t sender_uid)
 {
-       GVariant *param = NULL;
-       int result = APP2EXT_SUCCESS;
+       GVariant *param;
        char *pkgid = NULL;
        uid_t target_uid = -1;
-       int ret = 0;
+       int ret;
 
        g_variant_get(parameters, "(&si)", &pkgid, &target_uid);
 
        _D("pkgid(%s), sender_uid(%d), target_uid(%d)",
-               pkgid, sender_uid, target_uid);
+                       pkgid, sender_uid, target_uid);
 
        if (!_app2sd_server_check_permission(sender_uid, target_uid)) {
                _E("Not permitted user!");
                _app2sd_server_return_method_error(invocation,
-                       APP2EXT_ERROR_OPERATION_NOT_PERMITTED);
+                               APP2EXT_ERROR_OPERATION_NOT_PERMITTED);
                return;
        }
 
        ret = app2sd_usr_pre_app_uninstall(pkgid, target_uid);
-       if (ret) {
+       if (ret)
                _E("error(%d)", ret);
-               result = ret;
-       }
 
-       param = g_variant_new("(i)", result);
+       param = g_variant_new("(i)", ret);
        g_dbus_method_invocation_return_value(invocation, param);
 }
 
-static void _app2sd_server_post_app_uninstall(GDBusConnection *connection, const gchar *sender,
-       GVariant *parameters, GDBusMethodInvocation *invocation, uid_t sender_uid)
+static void _app2sd_server_post_app_uninstall(GDBusConnection *connection,
+               const gchar *sender, GVariant *parameters,
+               GDBusMethodInvocation *invocation, uid_t sender_uid)
 {
-       GVariant *param = NULL;
-       int result = APP2EXT_SUCCESS;
+       GVariant *param;
        char *pkgid = NULL;
        uid_t target_uid = -1;
-       int ret = 0;
+       int ret;
 
        g_variant_get(parameters, "(&si)", &pkgid, &target_uid);
 
        _D("pkgid(%s), sender_uid(%d), target_uid(%d)",
-               pkgid, sender_uid, target_uid);
+                       pkgid, sender_uid, target_uid);
 
        if (!_app2sd_server_check_permission(sender_uid, target_uid)) {
                _E("Not permitted user!");
                _app2sd_server_return_method_error(invocation,
-                       APP2EXT_ERROR_OPERATION_NOT_PERMITTED);
+                               APP2EXT_ERROR_OPERATION_NOT_PERMITTED);
                return;
        }
 
        ret = app2sd_usr_post_app_uninstall(pkgid, target_uid);
-       if (ret) {
+       if (ret)
                _E("error(%d)", ret);
-               result = ret;
-       }
 
-       param = g_variant_new("(i)", result);
+       param = g_variant_new("(i)", ret);
        g_dbus_method_invocation_return_value(invocation, param);
 }
 
-static void _app2sd_server_ondemand_setup_init(GDBusConnection *connection, const gchar *sender,
-       GVariant *parameters, GDBusMethodInvocation *invocation, uid_t sender_uid)
+static void _app2sd_server_ondemand_setup_init(GDBusConnection *connection,
+               const gchar *sender, GVariant *parameters,
+               GDBusMethodInvocation *invocation, uid_t sender_uid)
 {
-       GVariant *param = NULL;
-       int result = APP2EXT_SUCCESS;
+       GVariant *param;
        char *pkgid = NULL;
        uid_t target_uid = -1;
-       int ret = 0;
+       int ret;
 
        g_variant_get(parameters, "(&si)", &pkgid, &target_uid);
 
        _D("pkgid(%s), sender_uid(%d), target_uid(%d)",
-               pkgid, sender_uid, target_uid);
+                       pkgid, sender_uid, target_uid);
 
        if (!_app2sd_server_check_permission(sender_uid, target_uid)) {
                _E("Not permitted user!");
                _app2sd_server_return_method_error(invocation,
-                       APP2EXT_ERROR_OPERATION_NOT_PERMITTED);
+                               APP2EXT_ERROR_OPERATION_NOT_PERMITTED);
                return;
        }
 
        ret = app2sd_usr_on_demand_setup_init(pkgid, target_uid);
-       if (ret) {
+       if (ret)
                _E("error(%d)", ret);
-               result = ret;
-       }
 
-       param = g_variant_new("(i)", result);
+       param = g_variant_new("(i)", ret);
        g_dbus_method_invocation_return_value(invocation, param);
 }
 
-static void _app2sd_server_ondemand_setup_exit(GDBusConnection *connection, const gchar *sender,
-       GVariant *parameters, GDBusMethodInvocation *invocation, uid_t sender_uid)
+static void _app2sd_server_ondemand_setup_exit(GDBusConnection *connection,
+               const gchar *sender, GVariant *parameters,
+               GDBusMethodInvocation *invocation, uid_t sender_uid)
 {
-       GVariant *param = NULL;
-       int result = APP2EXT_SUCCESS;
+       GVariant *param;
        char *pkgid = NULL;
        uid_t target_uid = -1;
-       int ret = 0;
+       int ret;
 
        g_variant_get(parameters, "(&si)", &pkgid, &target_uid);
 
        _D("pkgid(%s), sender_uid(%d), target_uid(%d)",
-               pkgid, sender_uid, target_uid);
+                       pkgid, sender_uid, target_uid);
 
        if (!_app2sd_server_check_permission(sender_uid, target_uid)) {
                _E("Not permitted user!");
                _app2sd_server_return_method_error(invocation,
-                       APP2EXT_ERROR_OPERATION_NOT_PERMITTED);
+                               APP2EXT_ERROR_OPERATION_NOT_PERMITTED);
                return;
        }
 
        ret = app2sd_usr_on_demand_setup_exit(pkgid, target_uid);
-       if (ret) {
+       if (ret)
                _E("error(%d)", ret);
-               result = ret;
-       }
 
-       param = g_variant_new("(i)", result);
+       param = g_variant_new("(i)", ret);
        g_dbus_method_invocation_return_value(invocation, param);
 }
 
 static void _app2sd_server_pre_move_installed_app(GDBusConnection *connection,
-       const gchar *sender, GVariant *parameters,
-       GDBusMethodInvocation *invocation, uid_t sender_uid)
+               const gchar *sender, GVariant *parameters,
+               GDBusMethodInvocation *invocation, uid_t sender_uid)
 {
-       GVariant *param = NULL;
-       int result = APP2EXT_SUCCESS;
+       GVariant *param;
        int move_type;
        char *pkgid = NULL;
        GVariantIter *iter;
-       int ret = 0;
+       int ret;
        uid_t target_uid = -1;
-       GList *dir_list = NULL;
+       GList *dir_list;
        GList *list = NULL;
 
-       g_variant_get(parameters, "(&sia(si)i)", &pkgid, &move_type, &iter, &target_uid);
+       g_variant_get(parameters, "(&sia(si)i)", &pkgid, &move_type, &iter,
+                       &target_uid);
 
        _D("pkgid(%s), move_type(%d),sender_uid(%d), target_uid(%d)",
-               pkgid, move_type, sender_uid, target_uid);
+                       pkgid, move_type, sender_uid, target_uid);
 
        if (!_app2sd_server_check_permission(sender_uid, target_uid)) {
                _E("Not permitted user!");
                g_variant_iter_free(iter);
                _app2sd_server_return_method_error(invocation,
-                       APP2EXT_ERROR_OPERATION_NOT_PERMITTED);
+                               APP2EXT_ERROR_OPERATION_NOT_PERMITTED);
                return;
        }
 
-       result = _app2sd_server_generate_directory_list(iter, &list);
+       ret = _app2sd_server_generate_directory_list(iter, &list);
        g_variant_iter_free(iter);
+       if (ret) {
+               _E("error(%d)", ret);
+               _app2sd_server_return_method_error(invocation, ret);
+       }
 
        dir_list = g_list_first(list);
-       ret = app2sd_usr_pre_move_installed_app(pkgid, dir_list, move_type, target_uid);
-       if (ret) {
+       ret = app2sd_usr_pre_move_installed_app(pkgid, dir_list, move_type,
+                       target_uid);
+       if (ret)
                _E("pre_move error(%d)", ret);
-               result = ret;
-       }
 
        g_list_free_full(list, __free_dir_detail);
-       param = g_variant_new("(i)", result);
+       param = g_variant_new("(i)", ret);
        g_dbus_method_invocation_return_value(invocation, param);
 }
 
 static void _app2sd_server_post_move_installed_app(GDBusConnection *connection,
-       const gchar *sender, GVariant *parameters,
-       GDBusMethodInvocation *invocation, uid_t sender_uid)
+               const gchar *sender, GVariant *parameters,
+               GDBusMethodInvocation *invocation, uid_t sender_uid)
 {
-       GVariant *param = NULL;
-       int result = APP2EXT_SUCCESS;
+       GVariant *param;
        int move_type;
        char *pkgid = NULL;
        int ret = 0;
@@ -594,204 +589,187 @@ static void _app2sd_server_post_move_installed_app(GDBusConnection *connection,
        g_variant_get(parameters, "(&sii)", &pkgid, &move_type, &target_uid);
 
        _D("pkgid(%s), move_type(%d),sender_uid(%d), target_uid(%d)",
-               pkgid, move_type, sender_uid, target_uid);
+                       pkgid, move_type, sender_uid, target_uid);
 
        if (!_app2sd_server_check_permission(sender_uid, target_uid)) {
                _E("Not permitted user!");
                _app2sd_server_return_method_error(invocation,
-                       APP2EXT_ERROR_OPERATION_NOT_PERMITTED);
+                               APP2EXT_ERROR_OPERATION_NOT_PERMITTED);
                return;
        }
 
        ret = app2sd_usr_post_move_installed_app(pkgid, move_type, target_uid);
-       if (ret) {
+       if (ret)
                _E("post_move error(%d)", ret);
-               result = ret;
-       }
 
-       param = g_variant_new("(i)", result);
+       param = g_variant_new("(i)", ret);
        g_dbus_method_invocation_return_value(invocation, param);
 }
 
-static void _app2sd_server_force_clean(GDBusConnection *connection, const gchar *sender,
-       GVariant *parameters, GDBusMethodInvocation *invocation, uid_t sender_uid)
+static void _app2sd_server_force_clean(GDBusConnection *connection,
+               const gchar *sender, GVariant *parameters,
+               GDBusMethodInvocation *invocation, uid_t sender_uid)
 {
-       GVariant *param = NULL;
-       int result = APP2EXT_SUCCESS;
+       GVariant *param;
        char *pkgid = NULL;
        uid_t target_uid = -1;
-       int ret = 0;
+       int ret;
 
        g_variant_get(parameters, "(&si)", &pkgid, &target_uid);
 
        _D("pkgid(%s), sender_uid(%d), target_uid(%d)",
-               pkgid, sender_uid, target_uid);
+                       pkgid, sender_uid, target_uid);
 
        if (!_app2sd_server_check_permission(sender_uid, target_uid)) {
                _E("Not permitted user!");
                _app2sd_server_return_method_error(invocation,
-                       APP2EXT_ERROR_OPERATION_NOT_PERMITTED);
+                               APP2EXT_ERROR_OPERATION_NOT_PERMITTED);
                return;
        }
 
        ret = app2sd_usr_force_clean(pkgid, target_uid);
-       if (ret) {
+       if (ret)
                _E("error(%d)", ret);
-               result = ret;
-       }
 
-       param = g_variant_new("(i)", result);
+       param = g_variant_new("(i)", ret);
        g_dbus_method_invocation_return_value(invocation, param);
 }
 
-static void _app2sd_server_enable_full_pkg(GDBusConnection *connection, const gchar *sender,
-       GVariant *parameters, GDBusMethodInvocation *invocation, uid_t sender_uid)
+static void _app2sd_server_enable_full_pkg(GDBusConnection *connection,
+               const gchar *sender, GVariant *parameters,
+               GDBusMethodInvocation *invocation, uid_t sender_uid)
 {
-       GVariant *param = NULL;
-       int result = APP2EXT_SUCCESS;
-       int ret = 0;
+       GVariant *param;
+       int ret;
 
        _D("sender_uid(%d)", sender_uid);
 
        if (sender_uid >= REGULAR_USER) {
                _E("Not permitted user!");
                _app2sd_server_return_method_error(invocation,
-                       APP2EXT_ERROR_OPERATION_NOT_PERMITTED);
+                               APP2EXT_ERROR_OPERATION_NOT_PERMITTED);
                return;
        }
 
        ret = app2sd_enable_full_pkg();
-       if (ret) {
+       if (ret)
                _E("error(%d)", ret);
-               result = ret;
-       }
 
-       param = g_variant_new("(i)", result);
+       param = g_variant_new("(i)", ret);
        g_dbus_method_invocation_return_value(invocation, param);
 }
 
-static void _app2sd_server_disable_full_pkg(GDBusConnection *connection, const gchar *sender,
-       GVariant *parameters, GDBusMethodInvocation *invocation, uid_t sender_uid)
+static void _app2sd_server_disable_full_pkg(GDBusConnection *connection,
+               const gchar *sender, GVariant *parameters,
+               GDBusMethodInvocation *invocation, uid_t sender_uid)
 {
-       GVariant *param = NULL;
-       int result = APP2EXT_SUCCESS;
-       int ret = 0;
+       GVariant *param;
+       int ret;
 
        _D("sender_uid(%d)", sender_uid);
 
        if (sender_uid >= REGULAR_USER) {
                _E("Not permitted user!");
                _app2sd_server_return_method_error(invocation,
-                       APP2EXT_ERROR_OPERATION_NOT_PERMITTED);
+                               APP2EXT_ERROR_OPERATION_NOT_PERMITTED);
                return;
        }
 
        ret = app2sd_disable_full_pkg();
-       if (ret) {
+       if (ret)
                _E("error(%d)", ret);
-               result = ret;
-       }
 
-       param = g_variant_new("(i)", result);
+       param = g_variant_new("(i)", ret);
        g_dbus_method_invocation_return_value(invocation, param);
 }
 
 static void _app2sd_server_pre_migrate_legacy(GDBusConnection *connection,
-       const gchar *sender, GVariant *parameters,
-       GDBusMethodInvocation *invocation, uid_t sender_uid)
+               const gchar *sender, GVariant *parameters,
+               GDBusMethodInvocation *invocation, uid_t sender_uid)
 {
-       GVariant *param = NULL;
-       int result = APP2EXT_SUCCESS;
+       GVariant *param;
        char *pkgid = NULL;
        uid_t target_uid = -1;
-       int ret = 0;
+       int ret;
 
        g_variant_get(parameters, "(&si)", &pkgid, &target_uid);
 
        _D("pkgid(%s), target_uid(%d), sender_uid(%d)",
-               pkgid, target_uid, sender_uid);
+                       pkgid, target_uid, sender_uid);
 
        if (!_app2sd_server_check_permission(sender_uid, sender_uid)) {
                _E("Not permitted user!");
                _app2sd_server_return_method_error(invocation,
-                       APP2EXT_ERROR_OPERATION_NOT_PERMITTED);
+                               APP2EXT_ERROR_OPERATION_NOT_PERMITTED);
                return;
        }
 
        ret = app2sd_pre_migrate_legacy(pkgid, target_uid);
-       if (ret) {
+       if (ret)
                _E("error(%d)", ret);
-               result = ret;
-       }
 
-       param = g_variant_new("(i)", result);
+       param = g_variant_new("(i)", ret);
        g_dbus_method_invocation_return_value(invocation, param);
 }
 
 static void _app2sd_server_post_migrate_legacy(GDBusConnection *connection,
-       const gchar *sender, GVariant *parameters,
-       GDBusMethodInvocation *invocation, uid_t sender_uid)
+               const gchar *sender, GVariant *parameters,
+               GDBusMethodInvocation *invocation, uid_t sender_uid)
 {
-       GVariant *param = NULL;
-       int result = APP2EXT_SUCCESS;
+       GVariant *param;
        char *pkgid = NULL;
        uid_t target_uid = -1;
-       int ret = 0;
+       int ret;
 
        g_variant_get(parameters, "(&si)", &pkgid, &target_uid);
 
        _D("pkgid(%s), target_uid(%d), sender_uid(%d)",
-               pkgid, target_uid, sender_uid);
+                       pkgid, target_uid, sender_uid);
 
        if (!_app2sd_server_check_permission(sender_uid, sender_uid)) {
                _E("Not permitted user!");
                _app2sd_server_return_method_error(invocation,
-                       APP2EXT_ERROR_OPERATION_NOT_PERMITTED);
+                               APP2EXT_ERROR_OPERATION_NOT_PERMITTED);
                return;
        }
 
        ret = app2sd_post_migrate_legacy(pkgid, target_uid);
-       if (ret) {
+       if (ret)
                _E("error(%d)", ret);
-               result = ret;
-       }
 
-       param = g_variant_new("(i)", result);
+       param = g_variant_new("(i)", ret);
        g_dbus_method_invocation_return_value(invocation, param);
 }
 
 static void _app2sd_server_migrate_legacy_all(GDBusConnection *connection,
-       const gchar *sender, GVariant *parameters,
-       GDBusMethodInvocation *invocation, uid_t sender_uid)
+               const gchar *sender, GVariant *parameters,
+               GDBusMethodInvocation *invocation, uid_t sender_uid)
 {
-       GVariant *param = NULL;
-       int result = APP2EXT_SUCCESS;
-       int ret = 0;
+       GVariant *param;
+       int ret;
 
        _D("sender_uid(%d)", sender_uid);
 
        if (!_app2sd_server_check_permission(sender_uid, sender_uid)) {
                _E("Not permitted user!");
                _app2sd_server_return_method_error(invocation,
-                       APP2EXT_ERROR_OPERATION_NOT_PERMITTED);
+                               APP2EXT_ERROR_OPERATION_NOT_PERMITTED);
                return;
        }
 
        ret = app2sd_migrate_legacy_all();
-       if (ret) {
+       if (ret)
                _E("error(%d)", ret);
-               result = ret;
-       }
 
-       param = g_variant_new("(i)", result);
+       param = g_variant_new("(i)", ret);
        g_dbus_method_invocation_return_value(invocation, param);
 }
 
 static void handle_method_call(GDBusConnection *connection,
-       const gchar *sender, const gchar *object_path,
-       const gchar *interface_name, const gchar *method_name,
-       GVariant *parameters, GDBusMethodInvocation *invocation,
-       gpointer user_data)
+               const gchar *sender, const gchar *object_path,
+               const gchar *interface_name, const gchar *method_name,
+               GVariant *parameters, GDBusMethodInvocation *invocation,
+               gpointer user_data)
 {
        uid_t sender_uid = -1;
 
@@ -801,52 +779,52 @@ static void handle_method_call(GDBusConnection *connection,
 
        if (g_strcmp0(method_name, "PreAppInstall") == 0) {
                _app2sd_server_pre_app_install(connection, sender,
-                       parameters, invocation, sender_uid);
+                               parameters, invocation, sender_uid);
        } else if (g_strcmp0(method_name, "PostAppInstall") == 0) {
                _app2sd_server_post_app_install(connection, sender,
-                       parameters, invocation, sender_uid);
+                               parameters, invocation, sender_uid);
        } else if (g_strcmp0(method_name, "PreAppUpgrade") == 0) {
                _app2sd_server_pre_app_upgrade(connection, sender,
-                       parameters, invocation, sender_uid);
+                               parameters, invocation, sender_uid);
        } else if (g_strcmp0(method_name, "PostAppUpgrade") == 0) {
                _app2sd_server_post_app_upgrade(connection, sender,
-                       parameters, invocation, sender_uid);
+                               parameters, invocation, sender_uid);
        } else if (g_strcmp0(method_name, "PreAppUninstall") == 0) {
                _app2sd_server_pre_app_uninstall(connection, sender,
-                       parameters, invocation, sender_uid);
+                               parameters, invocation, sender_uid);
        } else if (g_strcmp0(method_name, "PostAppUninstall") == 0) {
                _app2sd_server_post_app_uninstall(connection, sender,
-                       parameters, invocation, sender_uid);
+                               parameters, invocation, sender_uid);
        } else if (g_strcmp0(method_name, "OndemandSetupInit") == 0) {
                _app2sd_server_ondemand_setup_init(connection, sender,
-                       parameters, invocation, sender_uid);
+                               parameters, invocation, sender_uid);
        } else if (g_strcmp0(method_name, "OndemandSetupExit") == 0) {
                _app2sd_server_ondemand_setup_exit(connection, sender,
-                       parameters, invocation, sender_uid);
+                               parameters, invocation, sender_uid);
        } else if (g_strcmp0(method_name, "PreMoveInstalledApp") == 0) {
                _app2sd_server_pre_move_installed_app(connection, sender,
-                       parameters, invocation, sender_uid);
+                               parameters, invocation, sender_uid);
        } else if (g_strcmp0(method_name, "PostMoveInstalledApp") == 0) {
                _app2sd_server_post_move_installed_app(connection, sender,
-                       parameters, invocation, sender_uid);
+                               parameters, invocation, sender_uid);
        } else if (g_strcmp0(method_name, "ForceClean") == 0) {
                _app2sd_server_force_clean(connection, sender,
-                       parameters, invocation, sender_uid);
+                               parameters, invocation, sender_uid);
        } else if (g_strcmp0(method_name, "EnableFullPkg") == 0) {
                _app2sd_server_enable_full_pkg(connection, sender,
-                       parameters, invocation, sender_uid);
+                               parameters, invocation, sender_uid);
        } else if (g_strcmp0(method_name, "DisableFullPkg") == 0) {
                _app2sd_server_disable_full_pkg(connection, sender,
-                       parameters, invocation, sender_uid);
+                               parameters, invocation, sender_uid);
        } else if (g_strcmp0(method_name, "PreMigrateLegacy") == 0) {
                _app2sd_server_pre_migrate_legacy(connection, sender,
-                       parameters, invocation, sender_uid);
+                               parameters, invocation, sender_uid);
        } else if (g_strcmp0(method_name, "PostMigrateLegacy") == 0) {
                _app2sd_server_post_migrate_legacy(connection, sender,
-                       parameters, invocation, sender_uid);
+                               parameters, invocation, sender_uid);
        } else if (g_strcmp0(method_name, "MigrateLegacyAll") == 0) {
                _app2sd_server_migrate_legacy_all(connection, sender,
-                       parameters, invocation, sender_uid);
+                               parameters, invocation, sender_uid);
        }
 
        if (source_id)
@@ -864,18 +842,19 @@ static const GDBusInterfaceVTable interface_vtable = {
 static void __app2sd_on_bus_acquired(GDBusConnection *connection,
                const gchar *name, gpointer user_data)
 {
-       _I("bus acquired(%s)", name);
-
-       guint reg_id = 0;
+       guint reg_id;
        GError *error = NULL;
 
+       _I("bus acquired(%s)", name);
+
        reg_id = g_dbus_connection_register_object(connection,
-               APP2SD_OBJECT_PATH,
-               introspection_data->interfaces[0],
-               &interface_vtable,
-               NULL, NULL, &error);
+                       APP2SD_OBJECT_PATH,
+                       introspection_data->interfaces[0],
+                       &interface_vtable,
+                       NULL, NULL, &error);
        if (reg_id == 0) {
-               _E("g_dbus_connection_register_object error(%s)", error->message);
+               _E("g_dbus_connection_register_object error(%s)",
+                               error->message);
                g_error_free(error);
        }
 }
@@ -893,13 +872,14 @@ static void __app2sd_on_name_lost(GDBusConnection *connection,
        g_main_loop_quit(app2sd_mainloop);
 }
 
-static int __app2sd_server_init()
+static int __app2sd_server_init(void)
 {
        GError *error = NULL;
-       guint owner_id = 0;
+       guint owner_id;
 
        /* gdbus setup for method call */
-       introspection_data = g_dbus_node_info_new_for_xml(introspection_xml, &error);
+       introspection_data = g_dbus_node_info_new_for_xml(introspection_xml,
+                       &error);
        if (!introspection_data) {
                _E("g_dbus_node_info_new_for_xml error(%s)", error->message);
                g_error_free(error);
@@ -907,12 +887,12 @@ static int __app2sd_server_init()
        }
 
        owner_id = g_bus_own_name(G_BUS_TYPE_SYSTEM,
-               APP2SD_BUS_NAME,
-               G_BUS_NAME_OWNER_FLAGS_NONE,
-               __app2sd_on_bus_acquired,
-               __app2sd_on_name_acquired,
-               __app2sd_on_name_lost,
-               NULL, NULL);
+                       APP2SD_BUS_NAME,
+                       G_BUS_NAME_OWNER_FLAGS_NONE,
+                       __app2sd_on_bus_acquired,
+                       __app2sd_on_name_acquired,
+                       __app2sd_on_name_lost,
+                       NULL, NULL);
        if (!owner_id) {
                _E("g_bus_own_name error");
                g_dbus_node_info_unref(introspection_data);
@@ -934,7 +914,7 @@ static void __app2sd_finalize(void)
 
 int main(int argc, char *argv[])
 {
-       int ret = 0;
+       int ret;
 
        _I("app2sd_server : start");