Add options to LUKS1 format
[platform/core/appfw/app2sd.git] / plugin / app2sd / server / app2sd_internals.c
index 66a75e9..c8d08f8 100644 (file)
  *
  */
 
+#define _GNU_SOURCE
 #include <dirent.h>
 #include <time.h>
 #include <pwd.h>
 
 #include "app2sd_internals.h"
 
-#ifdef TIZEN_FEATURE_APP2SD_DMCRYPT_ENCRYPTION
 #define DMCRYPT_ITER_TIME      50
 #define DMCRYPT_KEY_LEN                128
-#endif
 
 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) {
@@ -58,8 +57,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;
                }
        }
@@ -89,279 +87,14 @@ static int _app2sd_make_directory(const char *path, uid_t uid)
        return APP2EXT_SUCCESS;
 }
 
-char *_app2sd_find_associated_device_node(const char *loopback_device)
-{
-       char *ret_result = NULL;
-       char delims[] = ":";
-       char *result = NULL;
-       char dev[FILENAME_MAX] = { 0, };
-       char *devnode = NULL;
-
-       result = (char *)_app2sd_find_associated_device(loopback_device);
-       if (result == NULL) {
-               _D("there is no the associated file (%s)", loopback_device);
-               return NULL;
-       }
-
-       /* process the string*/
-       snprintf(dev, FILENAME_MAX - 1, "%s", result);
-
-       if (strstr(dev, "dev") == NULL) {
-               _E("unable to find the associated file");
-               free(result);
-               return NULL;
-       } else {
-               char *saveptr = NULL;
-               ret_result = strtok_r(dev, delims, &saveptr);
-               if (ret_result)
-                       devnode = strdup(ret_result);
-       }
-
-       free(result);
-
-       return devnode;
-}
-
-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;
-
-       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) {
-                       free(result);
-                       result = NULL;
-               }
-               count = 0;
-               char dev_path[BUF_SIZE] = { 0, };
-               snprintf(dev_path, BUF_SIZE, "/dev/loop%d", count);
-               while ((fp = fopen(dev_path, "r+")) != NULL) {
-                       count = count + 1;
-                       snprintf(dev_path, BUF_SIZE, "/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);
-
-               free(result);
-               result = NULL;
-
-       }
-       return ret_result;
-}
-
-char *_app2sd_do_loopback_encryption_setup(const char *pkgid,
-       const char *loopback_device, uid_t uid)
-{
-       int ret = APP2EXT_SUCCESS;
-       char *passwd = NULL;
-       char *result = NULL;
-       char *device_node = NULL;
-
-       if (pkgid == NULL) {
-               _E("invalid argument");
-               return NULL;
-       }
-
-       /* get password for loopback encryption */
-       ret = _app2sd_initialize_db();
-       if (ret) {
-               _E("app2sd db initialize failed");
-               return NULL;
-       }
-
-       if ((passwd = _app2sd_get_password_from_db(pkgid, uid)) == NULL) {
-               passwd = (char *)_app2sd_generate_password(pkgid);
-               if (NULL == passwd) {
-                       _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;
-                       }
-               }
-       }
-
-       /* get free device node*/
-       device_node = _app2sd_create_loopdevice_node();
-       if (NULL == device_node) {
-               free(passwd);
-               passwd = NULL;
-               _E("unable to find free loopback node");
-               return NULL;
-       }
-
-       _D("device_node (%s)", device_node);
-
-       result = (char *)_app2sd_encrypt_device(device_node,
-               loopback_device, 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;
-       }
-}
-
-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;
-
-       if (pkgid == NULL || temp_pkgid == 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) {
-               _E("unable to find free loopback node");
-               return NULL;
-       }
-       result = (char *)_app2sd_encrypt_device(device_node,
-               temp_loopback_device, passwd);
-       if (result == NULL) {
-               _E("encryption failed");
-               return NULL;
-       } else {
-               if (strlen(result) == 0) {
-                       free(result);
-                       result = NULL;
-                       return device_node;
-               } else {
-                       _E("error is (%s)", result);
-                       free(result);
-                       result = NULL;
-                       return NULL;
-               }
-       }
-
-       return device_node;
-}
-
-int _app2sd_remove_loopback_encryption_setup(const char *loopback_device)
-{
-       int ret = APP2EXT_SUCCESS;
-       char *result = NULL;
-       char *dev_node = NULL;
-
-       if ((dev_node = _app2sd_find_associated_device_node(loopback_device))
-               == NULL) {
-               _E("Unable to find the association");
-               ret = APP2EXT_ERROR_FIND_ASSOCIATED_DEVICE_NODE;
-       }
-
-       result = (char *)_app2sd_detach_loop_device(dev_node);
-       if (result == NULL) {
-               _E("error in detaching");
-               ret = APP2EXT_ERROR_DETACH_LOOPBACK_DEVICE;
-       } else {
-               free(result);
-               result = NULL;
-       }
-
-       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;
-       while (1) {
-               if ((dev_node =
-                       _app2sd_find_associated_device_node(loopback_device))
-                       == NULL) {
-                       _E("finish to find the association");
-                       ret = APP2EXT_SUCCESS;
-                       break;
-               }
-
-               _D("find node (%s)", dev_node);
-
-               result = (char *)_app2sd_detach_loop_device(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;
-               }
-       }
-
-       return ret;
-}
-
-#ifdef TIZEN_FEATURE_APP2SD_DMCRYPT_ENCRYPTION
 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];
+       const char *err_str;
 
        if (pkgid == NULL || loopback_device == NULL) {
                _E("invalid argument");
@@ -381,40 +114,36 @@ 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(pkgid);
-               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;
-                       }
                }
-       }
-
-       snprintf(dmcrypt_setup_cmd, BUF_SIZE, "/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);
 
-       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) {
+               ret = _app2sd_set_info_in_db(pkgid, passwd, loopback_device,
+                               uid);
+               if (ret < 0) {
+                       _E("unable to save password");
                        free(passwd);
-                       passwd = NULL;
+                       return APP2EXT_ERROR_SQLITE_REGISTRY;
                }
-               return APP2EXT_ERROR_SETUP_DMCRYPT_DEVICE;
        }
 
-       if (passwd) {
-               free(passwd);
-               passwd = NULL;
+       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 "
+                       "--type luks1 %s",
+                       passwd, DMCRYPT_ITER_TIME, DMCRYPT_KEY_LEN,
+                       loopback_device);
+       memset(passwd, 0, strlen(passwd));
+       free(passwd);
+       ret = system(dmcrypt_setup_cmd);
+       memset(dmcrypt_setup_cmd, 0, BUF_SIZE);
+       if (ret) {
+               err_str = strerror_r(errno, err_buf, sizeof(err_buf));
+               _E("Error setting up dmcrypt on app2sd file, error:%s, ret:%d",
+                               err_str, ret);
+               return APP2EXT_ERROR_SETUP_DMCRYPT_DEVICE;
        }
 
        return ret;
@@ -423,12 +152,13 @@ int _app2sd_dmcrypt_setup_device(const char *pkgid,
 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;
+       int size;
        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];
+       const char *err_str;
 
        if (pkgid == NULL || loopback_device == NULL) {
                _E("invalid argument");
@@ -436,9 +166,9 @@ int _app2sd_dmcrypt_open_device(const char *pkgid, const char *loopback_device,
        }
 
        if (is_dup)
-               snprintf(dev_name, BUF_SIZE, "%s.new_%d", pkgid, uid);
+               snprintf(dev_name, sizeof(dev_name), "%s.new_%d", pkgid, uid);
        else
-               snprintf(dev_name, BUF_SIZE, "%s_%d", pkgid, uid);
+               snprintf(dev_name, sizeof(dev_name), "%s_%d", pkgid, uid);
 
        /* get password for dmcrypt encryption */
        ret = _app2sd_initialize_db();
@@ -455,46 +185,62 @@ 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);
+               ret = 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);
+               if (ret < 0 || ret > sizeof(dmcrypt_open_cmd)) {
+                       free(passwd);
+                       _E("snprintf fail\n");
+                       return -1;
+               }
        } else {
-               snprintf(dmcrypt_open_cmd, sizeof(dmcrypt_open_cmd),
-                       "/bin/echo '%s' | /sbin/cryptsetup -q luksOpen %s %s",
-                       passwd, loopback_device, dev_name);
+               ret = snprintf(dmcrypt_open_cmd, sizeof(dmcrypt_open_cmd),
+                               "/bin/echo '%s' | /sbin/cryptsetup -q luksOpen "
+                               "%s %s",
+                               passwd, loopback_device, dev_name);
+               if (ret < 0 || ret > sizeof(dmcrypt_open_cmd)) {
+                       _E("snprintf fail\n");
+                       free(passwd);
+                       return -1;
+               }
        }
        free(passwd);
 
        ret = system(dmcrypt_open_cmd);
        if (ret) {
-               strerror_r(errno, buf, sizeof(buf));
-               _E("error opening dmcrypt device, error: [%s]", buf);
+               err_str = strerror_r(errno, buf, sizeof(buf));
+               _E("error opening dmcrypt device, error: [%s]", err_str);
                return APP2EXT_ERROR_OPEN_DMCRYPT_DEVICE;
        }
 
-       snprintf(buf, BUF_SIZE, "/dev/mapper/%s", dev_name);
-       len = strlen(buf);
-       *dev_node = (char *)calloc(len + 1, sizeof(char));
+       size = strlen("/dev/mapper/%s") + strlen(dev_name) + 1;
+       *dev_node = (char *)malloc(size);
        if (*dev_node == NULL) {
                _E("memory alloc failed");
                return APP2EXT_ERROR_OPEN_DMCRYPT_DEVICE;
        }
-       snprintf(*dev_node, len + 1, "%s", buf);
-
-       return ret;
+       ret = snprintf(*dev_node, size, "/dev/mapper/%s", dev_name);
+       if (ret < 0 || ret > size) {
+                       _E("snprintf fail\n");
+                       free(*dev_node);
+                       *dev_node = NULL;
+                       return -1;
+       }
+       return 0;
 }
 
 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];
+       const char *err_str;
+       char *t_dev_node;
 
        if (pkgid == NULL) {
-               _E("invalid argument\n");
+               _E("invalid argument");
                return APP2EXT_ERROR_INVALID_ARGUMENTS;
        }
 
@@ -505,15 +251,22 @@ int _app2sd_dmcrypt_close_device(const char *pkgid, uid_t uid)
        }
 
        free(t_dev_node);
-       t_dev_node = NULL;
 
-       snprintf(dev_node, BUF_SIZE, "/dev/mapper/%s_%d", pkgid, uid);
-       snprintf(dmcrypt_close_cmd, BUF_SIZE, "/sbin/cryptsetup -q luksClose %s", dev_node);
+       ret = snprintf(dev_node, sizeof(dev_node), "/dev/mapper/%s_%d", pkgid, uid);
+       if (ret < 0 || ret > sizeof(dev_node)) {
+               _E("snprintf fail");
+               return APP2EXT_ERROR_CLOSE_DMCRYPT_DEVICE;
+       }
+       ret = snprintf(dmcrypt_close_cmd, sizeof(dmcrypt_close_cmd),
+                       "/sbin/cryptsetup -q luksClose %s", dev_node);
+       if (ret < 0 || ret > sizeof(dmcrypt_close_cmd)) {
+               _E("snprintf fail");
+               return APP2EXT_ERROR_CLOSE_DMCRYPT_DEVICE;
+       }
        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);
+               err_str = strerror_r(errno, err_buf, sizeof(err_buf));
+               _E("error closing dmcrypt on app2sd file: %s", err_str);
                return APP2EXT_ERROR_CLOSE_DMCRYPT_DEVICE;
        }
 
@@ -522,27 +275,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;
-
-       snprintf(buf, BUF_SIZE, "/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) {
+       char *dev_node;
+       char buf[BUF_SIZE];
+
+       snprintf(buf, sizeof(buf), "/dev/mapper/%s_%d", pkgid, uid);
+       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;
 }
@@ -550,22 +296,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;
@@ -573,43 +320,43 @@ char *_app2sd_dmcrypt_duplicate_encryption_setup(const char *pkgid,
 
        return device_node;
 }
-#endif
 
 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, FILENAME_MAX - 1, "of=%s", loopback_device);
-       snprintf(buff, BUF_SIZE - 1, "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) {
@@ -626,9 +373,11 @@ 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 *err_str;
+       const char *argv[] = { "/sbin/mkfs.ext4", device_path, NULL };
 
        if (device_path == NULL) {
                _E("invalid param");
@@ -636,21 +385,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));
+               err_str = 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_str);
                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);
+               err_str = strerror_r(errno, err_buf, sizeof(err_buf));
+               _E("creating file system failed, error is (%s)", err_str);
                return APP2EXT_ERROR_CREATE_FS;
        }
        return ret;
@@ -659,29 +406,27 @@ 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 application_dir_mmc_path[FILENAME_MAX] = { 0, };
-       char application_dir_path[FILENAME_MAX] = { 0, };
+       int ret;
+       char app_dir_mmc_path[FILENAME_MAX];
+       char app_dir_path[FILENAME_MAX];
 
-       snprintf(application_dir_mmc_path, FILENAME_MAX - 1, "%s/.mmc/%s",
-               application_path, dir_name);
-       snprintf(application_dir_path, FILENAME_MAX, "%s/%s",
-               application_path, dir_name);
+       snprintf(app_dir_mmc_path, sizeof(app_dir_mmc_path), "%s/.mmc/%s",
+                       application_path, dir_name);
+       snprintf(app_dir_path, sizeof(app_dir_path), "%s/%s",
+                       application_path, dir_name);
 
-       ret = _app2sd_make_directory(application_dir_mmc_path, uid);
+       ret = _app2sd_make_directory(app_dir_mmc_path, uid);
        if (ret) {
                _E("create directory failed");
                return APP2EXT_ERROR_CREATE_DIRECTORY;
        }
 
-       if ((ret = symlink(application_dir_mmc_path,
-               application_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)",
-                               application_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;
                }
        }
@@ -692,21 +437,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;
 }
@@ -715,35 +458,42 @@ 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 application_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];
+       const char *err_str;
        struct timespec time = {
                .tv_sec = 0,
                .tv_nsec = 1000 * 1000 * 200
        };
+       char mountflags_str[BUF_SIZE];
+       const char *argv_mount[] = { "/bin/app2sd-mount-helper", dev,
+               app_mmc_path, FS_TYPE, mountflags_str, NULL };
 
-       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;
        }
        close(fd);
 
-       snprintf(application_mmc_path, FILENAME_MAX - 1, "%s/.mmc",
-               application_path);
-       fd = open(application_mmc_path, O_RDONLY|O_DIRECTORY);
+       ret = snprintf(app_mmc_path, sizeof(app_mmc_path), "%s/.mmc",
+                       application_path);
+       if (ret < 0 || ret > sizeof(app_mmc_path)) {
+               _E("snprintf fail");
+               return APP2EXT_ERROR_OPEN_DIR;
+       }
+       fd = open(app_mmc_path, O_RDONLY | O_DIRECTORY);
        if (fd < 0) {
-               _E("path(%s) error(%d)", application_mmc_path, errno);
+               _E("path(%s) error(%d)", app_mmc_path, errno);
                return APP2EXT_ERROR_OPEN_DIR;
        }
        close(fd);
@@ -753,47 +503,53 @@ int _app2sd_mount_app_content(const char *application_path, const char *pkgid,
 
        switch (mount_type) {
        case MOUNT_TYPE_RD:
-               if ((ret = mount(dev, application_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, application_mmc_path);
+               snprintf(mountflags_str, sizeof(mountflags_str), "%u",
+                               MS_MGC_VAL | MS_RDONLY | MS_NOSUID);
+               ret = _xsystem(argv_mount);
+               if (ret) {
+                       _E("read only mount failed, errono is (%d), "
+                                       "dev is (%s) path is (%s)",
+                                       ret, dev, app_mmc_path);
                        ret = APP2EXT_ERROR_MOUNT;
                }
                break;
        case MOUNT_TYPE_RW:
-               if ((ret = mount(dev, application_mmc_path, FS_TYPE,
-                       MS_MGC_VAL | MS_NOSUID, NULL)) < 0) {
-                       _E("read write mount failed, " \
-                               "errono is (%d)", errno);
+               snprintf(mountflags_str, sizeof(mountflags_str), "%u",
+                               MS_MGC_VAL | MS_NOSUID);
+               ret = _xsystem(argv_mount);
+               if (ret) {
+                       _E("read write mount failed, errno is (%d)", ret);
                        ret = APP2EXT_ERROR_MOUNT;
                }
                break;
        case MOUNT_TYPE_RW_NOEXEC:
-               if ((ret = mount(dev, application_mmc_path, FS_TYPE,
-                       MS_MGC_VAL | MS_NOEXEC | MS_NOSUID, NULL)) < 0) {
-                       _E("rwnx mount failed " \
-                               "errono is (%d)", errno);
+               snprintf(mountflags_str, sizeof(mountflags_str), "%u",
+                               MS_MGC_VAL | MS_NOEXEC | MS_NOSUID);
+               ret = _xsystem(argv_mount);
+               if (ret) {
+                       _E("rwnx mount failed errno is (%d)", ret);
                        ret = APP2EXT_ERROR_MOUNT;
                }
                break;
        case MOUNT_TYPE_RD_REMOUNT:
-               if ((ret = mount(dev, application_mmc_path, FS_TYPE,
-                       MS_MGC_VAL | MS_RDONLY | MS_REMOUNT | MS_NOSUID,
-                       NULL)) < 0) {
-                       _E("read remount failed "
-                               "errono is (%d)", errno);
+               snprintf(mountflags_str, sizeof(mountflags_str), "%u",
+                               MS_MGC_VAL | MS_RDONLY | MS_REMOUNT |
+                               MS_NOSUID);
+               ret = _xsystem(argv_mount);
+               if (ret) {
+                       _E("read remount failed errno is (%d)", ret);
                        ret = APP2EXT_ERROR_MOUNT;
                }
                break;
        case MOUNT_TYPE_RW_REMOUNT:
-               if ((ret = mount(dev, application_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;
+               snprintf(mountflags_str, sizeof(mountflags_str), "%u",
+                               MS_MGC_VAL | MS_REMOUNT | MS_NOSUID);
+               ret = _xsystem(argv_mount);
+               err_str = strerror_r(ret, err_buf, sizeof(err_buf));
+               if (ret) {
+                       _E("read write remount failed errno(%d), errstr(%s)",
+                                       ret, err_str);
+                       ret = APP2EXT_ERROR_MOUNT;
                }
                break;
        default:
@@ -802,16 +558,20 @@ 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, FILENAME_MAX - 1, "%s/lost+found",
-                               application_mmc_path);
+               ret = snprintf(temp_path, sizeof(temp_path), "%s/lost+found",
+                               app_mmc_path);
+               if (ret < 0 || ret > sizeof(temp_path)) {
+                       _E("snprintf fail");
+                       return APP2EXT_ERROR_CREATE_DIRECTORY;
+               }
                ret = _app2sd_make_directory(temp_path, uid);
                if (ret) {
                        _E("create directory(%s) failed", temp_path);
@@ -824,16 +584,18 @@ 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 application_dir_mmc_path[FILENAME_MAX] = { 0, };
-       char err_buf[1024] = {0,};
-
-       snprintf(application_dir_mmc_path, FILENAME_MAX - 1, "%s/.mmc",
-               application_path);
-       if ((ret = umount(application_dir_mmc_path)) < 0) {
-               strerror_r(errno, err_buf, sizeof(err_buf));
+       int ret;
+       char app_mmc_path[FILENAME_MAX];
+       char err_buf[1024];
+       const char *err_str;
+
+       snprintf(app_mmc_path, sizeof(app_mmc_path), "%s/.mmc",
+                       application_path);
+       ret = umount(app_mmc_path);
+       if (ret < 0) {
+               err_str = 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_str);
        }
 
        return ret;
@@ -841,18 +603,19 @@ 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];
+       const char *err_str;
 
        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));
+               err_str = 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_str);
                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;
@@ -864,91 +627,86 @@ 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 application_mmc_path[FILENAME_MAX] = { 0, };
-       char application_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;
-#if !defined(TIZEN_FEATURE_APP2SD_DMCRYPT_ENCRYPTION)
-       char *devi = NULL;
-#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, FILENAME_MAX - 1, "%s/%s/%s",
-                       mmc_path, EXTIMG_DIR, encoded_id);
-       free(encoded_id);
-       if (_is_global(uid)) {
-               snprintf(application_path, FILENAME_MAX - 1, "%s/%s",
-                       tzplatform_getenv(TZ_SYS_RW_APP), pkgid);
-       } else {
-               tzplatform_set_user(uid);
-               snprintf(application_path, FILENAME_MAX - 1, "%s/%s",
-                       tzplatform_getenv(TZ_USER_APP), pkgid);
-               tzplatform_reset_user();
-       }
+       int reqd_size;
+       int reqd_disk_size;
+       char *device_node;
+       int free_mmc_mem;
+       FILE *fp;
+       GList *list;
+       app2ext_dir_details *dir_detail;
+       char err_buf[1024];
+       const char *err_str;
+
+       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));
 
        /* 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(application_mmc_path, FILENAME_MAX - 1, "%s/.mmc",
-               application_path);
-       snprintf(application_archive_path, FILENAME_MAX - 1, "%s/.archive",
-               application_path);
+       ret = snprintf(app_mmc_path, sizeof(app_mmc_path), "%s/.mmc",
+                       application_path);
+       if (ret < 0 || ret > sizeof(app_mmc_path)) {
+               _E("snprintf fail");
+               return APP2EXT_ERROR_CREATE_DIRECTORY;
+       }
+       ret = snprintf(app_archive_path, sizeof(app_archive_path), "%s/.archive",
+                       application_path);
+       if (ret < 0 || ret > sizeof(app_archive_path)) {
+               _E("snprintf fail");
+               return APP2EXT_ERROR_CREATE_DIRECTORY;
+       }
 
-       ret = mkdir(application_mmc_path, mode);
+       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;
                }
        }
 
-       ret = mkdir(application_archive_path, mode);
+       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', FILENAME_MAX);
-                       snprintf(temp_dir_path, FILENAME_MAX,
-                                "%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);
+               if (dir_detail == NULL || dir_detail->name == NULL ||
+                               dir_detail->type != APP2EXT_DIR_RO)
+                       continue;
+               ret = snprintf(temp_dir_path, sizeof(temp_dir_path), "%s/%s",
+                               application_path, dir_detail->name);
+               if (ret < 0 || ret > sizeof(temp_dir_path)) {
+                       _E("snprintf fail");
+                       return APP2EXT_ERROR_MMC_STATUS;
                }
-               list = g_list_next(list);
+               _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;
@@ -964,19 +722,18 @@ 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;
        }
 
-#ifdef TIZEN_FEATURE_APP2SD_DMCRYPT_ENCRYPTION
        ret = _app2sd_dmcrypt_setup_device(pkgid, loopback_device, false, uid);
        if (ret) {
                _E("dmcrypt setup device error(%d)", ret);
@@ -984,31 +741,11 @@ 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;
        }
-#else
-       /* perform loopback encryption setup */
-       device_node = _app2sd_do_loopback_encryption_setup(pkgid,
-               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 */
-       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;
-       }
-#endif
        /* format the loopback file system */
        ret = _app2sd_create_file_system(device_node);
        if (ret) {
@@ -1017,83 +754,73 @@ 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, FILENAME_MAX,
-                                "%s/%s", application_path,
-                               dir_detail->name);
-                       _D("app2archive, temp_dir_path(%s)",
-                               temp_dir_path);
-                       ret = _app2sd_move_to_archive(temp_dir_path,
-                               application_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,
-                                            application_archive_path);
-                               }
-                               goto ERR;
-                       }
+               if (dir_detail == NULL || dir_detail->name == NULL ||
+                               dir_detail->type != APP2EXT_DIR_RO)
+                       continue;
+               ret = snprintf(temp_dir_path, sizeof(temp_dir_path), "%s/%s",
+                               application_path, dir_detail->name);
+               if (ret < 0 || ret > sizeof(temp_dir_path)) {
+                       _E("snprintf fail");
+                       ret = APP2EXT_ERROR_MOVE;
+                       goto ERR;
+               }
+               _D("app2archive, temp_dir_path(%s)", temp_dir_path);
+               ret = _app2sd_move_to_archive(temp_dir_path, app_archive_path);
+               if (ret) {
+                       _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', FILENAME_MAX);
-                       snprintf(temp_dir_path, FILENAME_MAX,
-                               "%s/%s", application_archive_path,
-                               dir_detail->name);
-                       _D("archive2mmc, temp_dir_path(%s)",
-                               temp_dir_path);
-                       ret = _app2sd_copy_dir(temp_dir_path,
-                               application_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,
-                                               application_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;
+               if (dir_detail == NULL || dir_detail->name == NULL ||
+                               dir_detail->type != APP2EXT_DIR_RO)
+                       continue;
+               ret = snprintf(temp_dir_path, sizeof(temp_dir_path), "%s/%s",
+                               app_archive_path, dir_detail->name);
+               if (ret < 0 || ret > sizeof(temp_dir_path)) {
+                       _E("snprintf fail");
+                       ret = APP2EXT_ERROR_COPY_DIRECTORY;
+                       goto ERR;
+               }
+               _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 {
+                               err_str = 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_str);
                        }
+                       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(application_archive_path);
+       ret = _app2sd_delete_directory(app_archive_path);
        if (ret) {
-               _E("unable to delete (%s)", application_archive_path);
+               _E("unable to delete (%s)", app_archive_path);
                ret = APP2EXT_ERROR_DELETE_DIRECTORY;
                goto ERR;
        }
@@ -1102,72 +829,152 @@ 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;
 }
 
+static int __check_storage_availability(
+               const char *loopback_device, int free_internal_mem) {
+       unsigned long long temp;
+       int reqd_size;
+
+       temp = _app2sd_calculate_file_size(loopback_device);
+       reqd_size = (int)((temp) / (1024 * 1024));
+       _D("reqd size is (%d)", reqd_size);
+
+       if (reqd_size == 0) {
+               _E("app entry is not present in SD Card");
+               return APP2EXT_ERROR_LOOPBACK_DEVICE_UNAVAILABLE;
+       }
+
+       _I("reqd size: (%d)MB, free internal mem: (%d)MB",
+                       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"
+                       " for application installation");
+               return APP2EXT_ERROR_MMC_INSUFFICIENT_MEMORY;
+       }
+
+       return 0;
+}
+
+static int __copy_directories(const char *app_mmc_path, const char *app_archive_path,
+               const char *application_path, app2ext_dir_details *dir_detail)
+{
+       int ret;
+       char temp_dir_path[FILENAME_MAX];
+       char err_buf[1024];
+       const char *err_str;
+
+       ret = snprintf(temp_dir_path, sizeof(temp_dir_path), "%s/%s",
+                       app_mmc_path, dir_detail->name);
+       if (ret < 0 || ret > sizeof(temp_dir_path)) {
+               _E("snprintf fail");
+               return -1;
+       }
+       _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 {
+                       err_str = 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_str);
+               }
+               return -1;
+       }
+       /* delete the symbolic link files [bin, lib, res]*/
+       ret = snprintf(temp_dir_path, sizeof(temp_dir_path), "%s/%s",
+                       application_path, dir_detail->name);
+       if (ret < 0 || ret > sizeof(temp_dir_path)) {
+               _E("snprintf fail");
+               return -1;
+       }
+       _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);
+                       return -1;
+               }
+       }
+       /* Copy content to destination */
+       ret = snprintf(temp_dir_path, sizeof(temp_dir_path), "%s/%s",
+                       app_archive_path, dir_detail->name);
+       if (ret < 0 || ret > sizeof(temp_dir_path)) {
+               _E("snprintf fail");
+               return -1;
+       }
+       _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 {
+                       err_str = 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_str);
+               }
+               return -1;
+       }
+       return 0;
+}
+
 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 application_mmc_path[FILENAME_MAX] = { 0, };
-       char application_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;
-       struct statvfs buf = {0,};
-       unsigned long long temp = 0;
-       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, FILENAME_MAX - 1, "%s/%s/%s",
-                       mmc_path, EXTIMG_DIR, encoded_id);
-       free(encoded_id);
-       if (_is_global(uid)) {
-               snprintf(application_path, FILENAME_MAX - 1, "%s/%s",
-                       tzplatform_getenv(TZ_SYS_RW_APP), pkgid);
-       } else {
-               tzplatform_set_user(uid);
-               snprintf(application_path, FILENAME_MAX - 1, "%s/%s",
-                       tzplatform_getenv(TZ_USER_APP), pkgid);
-               tzplatform_reset_user();
-       }
+       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 free_internal_mem;
+       int mount_type;
+
+       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));
 
        /* check whether application is in external memory or not */
        fp = fopen(loopback_device, "r+");
        if (fp == NULL) {
                _E("application (%s) is not installed on SD Card",
-                    pkgid);
+                               pkgid);
                return APP2EXT_ERROR_FILE_ABSENT;
-       } else {
-               fclose(fp);
-               fp = NULL;
        }
+       fclose(fp);
 
-       memset((void *)&buf, '\0', sizeof(struct statvfs));
-       ret = statvfs(INTERNAL_STORAGE_PATH, &buf);
-       if (0 == ret) {
-               temp = (buf.f_bsize * buf.f_bavail) / (1024 * 1024);
-               free_internal_mem = (int)temp;
-       } else {
-               _E("unable to get internal storage size");
-               return APP2EXT_ERROR_MMC_INSUFFICIENT_MEMORY;
+       /* find avialable free memory in the internal storage */
+       ret = _app2sd_get_available_free_memory(INTERNAL_STORAGE_PATH,
+                       &free_internal_mem);
+       if (ret) {
+               _E("unable to get available free memory in internal (%d)", ret);
+               return APP2EXT_ERROR_MMC_STATUS;
        }
 
        fp = fopen(loopback_device, "r+");
@@ -1178,187 +985,93 @@ static int _app2sd_move_app_to_internal(const char *pkgid, GList *dir_list,
        fclose(fp);
 
        /* get installed app size*/
-       temp = _app2sd_calculate_file_size(loopback_device);
-       reqd_size = (int)((temp) / (1024 * 1024));
-       _D("reqd size is (%d)", reqd_size);
-
-       if (reqd_size == 0) {
-               _E("app entry is not present in SD Card");
-               return APP2EXT_ERROR_LOOPBACK_DEVICE_UNAVAILABLE;
-       }
-
-       _D("reqd size: (%d)MB, free internal mem: (%d)MB",
-               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" \
-                       " for application installation (%d)", ret);
-               return APP2EXT_ERROR_MMC_INSUFFICIENT_MEMORY;
-       }
+       ret = __check_storage_availability(loopback_device, free_internal_mem);
+       if (ret)
+               return ret;
 
-#ifdef TIZEN_FEATURE_APP2SD_DMCRYPT_ENCRYPTION
        device_node =
                _app2sd_find_associated_dmcrypt_device_node(pkgid, 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;
                }
-#else
-               device_node = _app2sd_do_loopback_encryption_setup(pkgid,
-                       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(application_mmc_path, FILENAME_MAX - 1, "%s/.mmc",
-               application_path);
-       snprintf(application_archive_path, FILENAME_MAX - 1, "%s/.archive",
-               application_path);
+       ret = snprintf(app_mmc_path, sizeof(app_mmc_path), "%s/.mmc",
+                       application_path);
+       if (ret < 0 || ret > sizeof(app_mmc_path)) {
+               _E("snprintf fail");
+               ret = -1;
+               goto ERR;
+       }
+       ret = snprintf(app_archive_path, sizeof(app_archive_path), "%s/.archive",
+                       application_path);
+       if (ret < 0 || ret > sizeof(app_archive_path)) {
+               _E("snprintf fail");
+               ret = -1;
+               goto ERR;
+       }
 
-       ret = mkdir(application_archive_path, mode);
+       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', FILENAME_MAX);
-                       snprintf(temp_dir_path, FILENAME_MAX,
-                                "%s/%s", application_mmc_path,
-                                dir_detail->name);
-                       _D("mmc2archive, temp_dir_path(%s)", temp_dir_path);
-                       ret = _app2sd_copy_dir(temp_dir_path,
-                                    application_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,
-                                               application_archive_path, err_buf);
-                               }
-                               goto ERR;
-                       }
-
-                       /* delete the symbolic link files [bin, lib, res]*/
-                       memset(temp_dir_path, '\0', FILENAME_MAX);
-                       snprintf(temp_dir_path, FILENAME_MAX,
-                                "%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",
-                                               temp_dir_path);
-                                       goto ERR;
-                               }
-                       }
-
-                       /* Copy content to destination */
-                       memset(temp_dir_path, '\0', FILENAME_MAX);
-                       snprintf(temp_dir_path, FILENAME_MAX,
-                               "%s/%s", application_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;
-                       }
+               if (dir_detail == NULL || dir_detail->name == NULL ||
+                               dir_detail->type != APP2EXT_DIR_RO)
+                       continue;
+               /* archiving code */
+               ret = __copy_directories(app_mmc_path, app_archive_path,
+                               application_path, dir_detail);
+               if (ret != 0) {
+                       ret = -1;
+                       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);
        if (ret)
                _E("close dmcrypt device error(%d)", ret);
-#else
-       ret = _app2sd_remove_loopback_encryption_setup(loopback_device);
-       if (ret)
-               _E("unable to detach loopback setup for (%s)",
-                    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(application_mmc_path);
+       ret = _app2sd_delete_directory(app_mmc_path);
        if (ret)
-               _E("unable to delete (%s)", application_mmc_path);
+               _E("unable to delete (%s)", app_mmc_path);
 
-       ret = _app2sd_delete_directory(application_archive_path);
+       ret = _app2sd_delete_directory(app_archive_path);
        if (ret)
-               _E("unable to delete (%s)", application_archive_path);
+               _E("unable to delete (%s)", app_archive_path);
 
        /* remove passwrd from DB */
        ret = _app2sd_initialize_db();
@@ -1372,10 +1085,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;
 }
@@ -1383,12 +1094,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;
@@ -1396,7 +1107,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;
@@ -1412,118 +1123,58 @@ 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', FILENAME_MAX);
-                       snprintf(path, FILENAME_MAX - 1, "%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;
-#if !defined(TIZEN_FEATURE_APP2SD_DMCRYPT_ENCRYPTION)
-       char *devi = NULL;
-       char *result = NULL;
-       char *passwd = NULL;
-#endif
+       int ret;
+       int err_res;
 
        /* 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;
        }
 
        /* 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);
                return APP2EXT_ERROR_SETUP_DMCRYPT_DEVICE;
        }
-#else
-       /* get password for loopback encryption */
-       ret = _app2sd_initialize_db();
-       if (ret) {
-               _E("app2sd db initialize failed");
-               return APP2EXT_ERROR_DB_INITIALIZE;
-       }
-
-       if ((passwd = _app2sd_get_password_from_db(pkgid, uid)) == NULL) {
-               passwd = (char *)_app2sd_generate_password(pkgid);
-               if (NULL == passwd) {
-                       _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 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);
-       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
-        * device node or not
-        */
-       devi = _app2sd_find_associated_device_node(temp_loopback_device);
-       if (devi == NULL) {
-               _E("finding associated device node failed");
-               err_res = APP2EXT_ERROR_DO_LOSETUP;
-               goto FINISH_OFF;
-       }
-       _D("losetup SUCCESS");
-#endif
 
        /* format the loopback file system */
        ret = _app2sd_create_file_system(dev_node);
@@ -1536,78 +1187,54 @@ 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:
        if (dev_node) {
-#ifdef TIZEN_FEATURE_APP2SD_DMCRYPT_ENCRYPTION
                ret = _app2sd_dmcrypt_close_device(temp_pkgid, uid);
                if (ret)
                        _E("close dmcrypt device error(%d)", ret);
-#else
-               result = _app2sd_detach_loop_device(dev_node);
-               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 application_mmc_path[FILENAME_MAX] = { 0, };
-       char temp_application_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;
        }
 
        /* get the associated device node for SD card applicatione */
-#ifdef TIZEN_FEATURE_APP2SD_DMCRYPT_ENCRYPTION
        old_device_node =
                _app2sd_find_associated_dmcrypt_device_node(pkgid, uid);
-#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,
                        false, uid, &old_device_node);
                if (ret) {
@@ -1615,41 +1242,28 @@ int _app2sd_update_loopback_device_size(const char *pkgid,
                        err_res = APP2EXT_ERROR_OPEN_DMCRYPT_DEVICE;
                        goto FINISH_OFF;
                }
-#else
-               old_device_node = _app2sd_do_loopback_encryption_setup(pkgid,
-                       loopback_device, uid);
-               if (old_device_node == NULL) {
-                       _E("loopback encryption setup failed");
-                       err_res = APP2EXT_ERROR_DO_LOSETUP;
-                       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;
        }
+       free(old_device_node);
 
-       snprintf(application_mmc_path, FILENAME_MAX - 1,
-                "%s/.mmc", application_path);
-       snprintf(temp_application_mmc_path, FILENAME_MAX - 1,
-               "%s/.mmc", temp_application_path);
+       snprintf(app_mmc_path, sizeof(app_mmc_path),
+                       "%s/.mmc", application_path);
+       snprintf(temp_app_mmc_path, sizeof(temp_app_mmc_path),
+                       "%s/.mmc", temp_application_path);
 
-       ret = _app2sd_copy_ro_content(application_mmc_path,
-               temp_application_mmc_path, dir_list);
+       ret = _app2sd_copy_ro_content(app_mmc_path,
+                       temp_app_mmc_path, dir_list);
        if (ret) {
                _E("copy ro content failed");
                err_res = ret;
@@ -1668,7 +1282,6 @@ int _app2sd_update_loopback_device_size(const char *pkgid,
                goto FINISH_OFF;
        }
 
-#ifdef TIZEN_FEATURE_APP2SD_DMCRYPT_ENCRYPTION
        ret = _app2sd_dmcrypt_close_device(pkgid, uid);
        if (ret) {
                _E("close dmcrypt device error(%d)", ret);
@@ -1682,20 +1295,6 @@ int _app2sd_update_loopback_device_size(const char *pkgid,
                err_res = APP2EXT_ERROR_CLOSE_DMCRYPT_DEVICE;
                goto FINISH_OFF;
        }
-#else
-       ret = _app2sd_remove_loopback_encryption_setup(loopback_device);
-       if (ret) {
-               _E("unable to remove loopback setup");
-               err_res = APP2EXT_ERROR_DELETE_LOOPBACK_DEVICE;
-       }
-
-       ret = _app2sd_remove_loopback_encryption_setup(temp_loopback_device);
-       if (ret) {
-               _E("unable to remove loopback setup");
-               err_res = APP2EXT_ERROR_DELETE_LOOPBACK_DEVICE;
-               goto FINISH_OFF;
-       }
-#endif
 
        ret = _app2sd_delete_directory(loopback_device);
        if (ret) {
@@ -1721,29 +1320,13 @@ int _app2sd_update_loopback_device_size(const char *pkgid,
        return APP2EXT_SUCCESS;
 
 FINISH_OFF:
-       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);
        if (ret)
                _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
-       ret = _app2sd_remove_loopback_encryption_setup(loopback_device);
-       if (ret)
-               _E("unable to remove loopback setup");
-
-       ret = _app2sd_remove_loopback_encryption_setup(temp_loopback_device);
-       if (ret)
-               _E("unable to remove loopback setup");
-#endif
 
        _app2sd_delete_loopback_device(temp_loopback_device);
 
@@ -1757,28 +1340,25 @@ 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);
 
        /* detach the loopback encryption setup for the application */
-#ifdef TIZEN_FEATURE_APP2SD_DMCRYPT_ENCRYPTION
        ret = _app2sd_dmcrypt_close_device(pkgid, uid);
        if (ret)
                _W("close dmcrypt device error (%d)", ret);
-#else
-       ret = _app2sd_remove_all_loopback_encryption_setups(loopback_device);
-       if (ret)
-               _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);
@@ -1792,6 +1372,5 @@ int _app2sd_force_clean(const char *pkgid, const char *application_path,
        if (ret)
                _W("cannot remove info from db");
 
-
        return ret;
 }