Add options to LUKS1 format
[platform/core/appfw/app2sd.git] / plugin / app2sd / server / app2sd_internals.c
index 4133c9f..c8d08f8 100644 (file)
@@ -94,6 +94,7 @@ int _app2sd_dmcrypt_setup_device(const char *pkgid,
        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");
@@ -130,15 +131,18 @@ int _app2sd_dmcrypt_setup_device(const char *pkgid,
 
        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",
+                       "-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) {
-               strerror_r(errno, err_buf, sizeof(err_buf));
+               err_str = strerror_r(errno, err_buf, sizeof(err_buf));
                _E("Error setting up dmcrypt on app2sd file, error:%s, ret:%d",
-                               err_buf, ret);
+                               err_str, ret);
                return APP2EXT_ERROR_SETUP_DMCRYPT_DEVICE;
        }
 
@@ -149,10 +153,12 @@ int _app2sd_dmcrypt_open_device(const char *pkgid, const char *loopback_device,
                bool is_dup, uid_t uid, char **dev_node)
 {
        int ret;
+       int size;
        char *passwd;
        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");
@@ -179,33 +185,49 @@ 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),
+               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),
+               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, sizeof(buf), "/dev/mapper/%s", dev_name);
-       *dev_node = strdup(buf);
+       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;
        }
-
-       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)
@@ -214,6 +236,7 @@ int _app2sd_dmcrypt_close_device(const char *pkgid, uid_t uid)
        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) {
@@ -229,13 +252,21 @@ int _app2sd_dmcrypt_close_device(const char *pkgid, uid_t uid)
 
        free(t_dev_node);
 
-       snprintf(dev_node, sizeof(dev_node), "/dev/mapper/%s_%d", pkgid, uid);
-       snprintf(dmcrypt_close_cmd, sizeof(dmcrypt_close_cmd),
+       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: %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;
        }
 
@@ -345,6 +376,7 @@ int _app2sd_create_file_system(const char *device_path)
        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) {
@@ -355,17 +387,17 @@ int _app2sd_create_file_system(const char *device_path)
        /* Format the filesystem [create a filesystem]*/
        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;
        }
        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;
@@ -431,10 +463,14 @@ int _app2sd_mount_app_content(const char *application_path, const char *pkgid,
        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 (dev == NULL) {
                _E("input param is NULL (%s)", dev);
@@ -449,8 +485,12 @@ int _app2sd_mount_app_content(const char *application_path, const char *pkgid,
        }
        close(fd);
 
-       snprintf(app_mmc_path, sizeof(app_mmc_path), "%s/.mmc",
+       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)", app_mmc_path, errno);
@@ -463,47 +503,52 @@ int _app2sd_mount_app_content(const char *application_path, const char *pkgid,
 
        switch (mount_type) {
        case MOUNT_TYPE_RD:
-               ret = mount(dev, app_mmc_path, FS_TYPE,
-                               MS_MGC_VAL | MS_RDONLY | MS_NOSUID, NULL);
-               if (ret < 0) {
+               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)",
-                                       errno, dev, app_mmc_path);
+                                       ret, dev, app_mmc_path);
                        ret = APP2EXT_ERROR_MOUNT;
                }
                break;
        case MOUNT_TYPE_RW:
-               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);
+               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:
-               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);
+               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:
-               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);
+               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:
-               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);
+               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;
@@ -521,8 +566,12 @@ int _app2sd_mount_app_content(const char *application_path, const char *pkgid,
        if (mount_type != MOUNT_TYPE_RD &&
                        mount_type != MOUNT_TYPE_RD_REMOUNT) {
                /* change lost+found permission */
-               snprintf(temp_path, sizeof(temp_path), "%s/lost+found",
+               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);
@@ -538,14 +587,15 @@ int _app2sd_unmount_app_content(const char *application_path)
        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) {
-               strerror_r(errno, err_buf, sizeof(err_buf));
+               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;
@@ -555,12 +605,13 @@ static int _app2sd_move_to_archive(const char *src_path, const char *arch_path)
 {
        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) {
-               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;
        }
 
@@ -592,6 +643,7 @@ static int _app2sd_move_app_to_external(const char *pkgid, GList *dir_list,
        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));
@@ -611,10 +663,18 @@ static int _app2sd_move_app_to_external(const char *pkgid, GList *dir_list,
                                uid);
        }
 
-       snprintf(app_mmc_path, sizeof(app_mmc_path), "%s/.mmc",
+       ret = snprintf(app_mmc_path, sizeof(app_mmc_path), "%s/.mmc",
                        application_path);
-       snprintf(app_archive_path, sizeof(app_archive_path), "%s/.archive",
+       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(app_mmc_path, mode);
        if (ret) {
@@ -639,8 +699,12 @@ static int _app2sd_move_app_to_external(const char *pkgid, GList *dir_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",
+               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;
+               }
                _D("cal size of app dirs, temp_dir_path(%s)", temp_dir_path);
                total_size += _app2sd_calculate_dir_size(temp_dir_path);
        }
@@ -695,8 +759,13 @@ static int _app2sd_move_app_to_external(const char *pkgid, GList *dir_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",
+               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) {
@@ -721,18 +790,24 @@ static int _app2sd_move_app_to_external(const char *pkgid, GList *dir_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",
+               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 {
-                               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),"
                                                " error is (%s)", temp_dir_path,
-                                               app_mmc_path, err_buf);
+                                               app_mmc_path, err_str);
                        }
                        goto ERR;
                }
@@ -761,12 +836,111 @@ ERR:
        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;
        mode_t mode = DIR_PERMS;
-       char temp_dir_path[FILENAME_MAX];
        char app_mmc_path[FILENAME_MAX];
        char app_archive_path[FILENAME_MAX];
        char application_path[FILENAME_MAX];
@@ -775,10 +949,7 @@ static int _app2sd_move_app_to_internal(const char *pkgid, GList *dir_list,
        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];
        int mount_type;
 
        ret = _app2sd_get_loopback_device_path(mmc_path, pkgid, uid,
@@ -787,13 +958,13 @@ static int _app2sd_move_app_to_internal(const char *pkgid, GList *dir_list,
                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) {
                _E("application (%s) is not installed on SD Card",
-                    pkgid);
+                               pkgid);
                return APP2EXT_ERROR_FILE_ABSENT;
        }
        fclose(fp);
@@ -814,26 +985,9 @@ 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;
-       }
-
-       _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 (%d)", ret);
-               return APP2EXT_ERROR_MMC_INSUFFICIENT_MEMORY;
-       }
+       ret = __check_storage_availability(loopback_device, free_internal_mem);
+       if (ret)
+               return ret;
 
        device_node =
                _app2sd_find_associated_dmcrypt_device_node(pkgid, uid);
@@ -859,10 +1013,20 @@ static int _app2sd_move_app_to_internal(const char *pkgid, GList *dir_list,
                goto ERR;
        }
 
-       snprintf(app_mmc_path, sizeof(app_mmc_path), "%s/.mmc",
+       ret = snprintf(app_mmc_path, sizeof(app_mmc_path), "%s/.mmc",
                        application_path);
-       snprintf(app_archive_path, sizeof(app_archive_path), "%s/.archive",
+       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(app_archive_path, mode);
        if (ret) {
@@ -880,50 +1044,10 @@ static int _app2sd_move_app_to_internal(const char *pkgid, GList *dir_list,
                                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]*/
-               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;
-                       }
-               }
-               /* 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);
-                       }
+               ret = __copy_directories(app_mmc_path, app_archive_path,
+                               application_path, dir_detail);
+               if (ret != 0) {
+                       ret = -1;
                        goto ERR;
                }
        }
@@ -1131,6 +1255,7 @@ int _app2sd_update_loopback_device_size(const char *pkgid,
                _E("remount failed");
                err_res = APP2EXT_ERROR_MOUNT_PATH;
        }
+       free(old_device_node);
 
        snprintf(app_mmc_path, sizeof(app_mmc_path),
                        "%s/.mmc", application_path);
@@ -1195,9 +1320,6 @@ int _app2sd_update_loopback_device_size(const char *pkgid,
        return APP2EXT_SUCCESS;
 
 FINISH_OFF:
-       if (old_device_node)
-               free(old_device_node);
-
        ret = _app2sd_dmcrypt_close_device(pkgid, uid);
        if (ret)
                _E("close dmcrypt device error(%d)", ret);