Fix wrong available mem calculation of internal storage
[platform/core/appfw/app2sd.git] / plugin / app2sd / server / app2sd_internals_utils.c
index 1640af3..705d52f 100644 (file)
@@ -22,7 +22,7 @@
  */
 
 #include <dirent.h>
-#include <time.h>
+#include <glib.h>
 #include <storage-internal.h>
 
 #include "app2sd_internals.h"
@@ -106,8 +106,8 @@ int _app2sd_check_mmc_status(char **sdpath)
 }
 
 /*
- * This function returns the available free memory in the SD Card.
- * param [in]: sd_path: This is sd card access path.
+ * This function returns the available free memory in the storage.
+ * param [in]: mmc_path: This is storage access path.
  * param [out]: free_mem: Result will be available in this.
  * User has to pass valid memory address.
  * return: On success, it will return 0.
@@ -128,7 +128,7 @@ int _app2sd_get_available_free_memory(char *mmc_path, int *free_mem)
 
        ret = statvfs(mmc_path, &buf);
        if (ret) {
-               _E("unable to get SD Card memory information");
+               _E("unable to get memory information");
                return APP2EXT_ERROR_MMC_INFORMATION;
        }
 
@@ -142,22 +142,19 @@ void _app2sd_delete_symlink(const char *dirname)
 {
        int ret = 0;
        DIR *dp = NULL;
-       struct dirent ep;
-       struct dirent *er = NULL;
+       struct dirent *ep = NULL;
        char abs_filename[FILENAME_MAX] = { 0, };
 
-       _D("start clean_symlink [%s]", dirname);
-
        dp = opendir(dirname);
        if (dp != NULL) {
-               while (readdir_r(dp, &ep, &er) == 0 && er != NULL) {
+               while ((ep = readdir(dp)) != NULL) {
                        char mmc_path[PATH_MAX] = {0};
 
-                       if (!strcmp(ep.d_name, ".") || !strcmp(ep.d_name, ".."))
+                       if (!strcmp(ep->d_name, ".") || !strcmp(ep->d_name, ".."))
                                continue;
 
                        /*get realpath find symlink to ".mmc" and unlink it*/
-                       snprintf(abs_filename, FILENAME_MAX, "%s/%s", dirname, ep.d_name);
+                       snprintf(abs_filename, FILENAME_MAX, "%s/%s", dirname, ep->d_name);
                        char *path = realpath(abs_filename, mmc_path);
                        if (!path)
                                _E("realpath failed");
@@ -180,11 +177,7 @@ void _app2sd_delete_symlink(const char *dirname)
                ret = remove(abs_filename);
                if (ret == -1)
                        return;
-       } else {
-               _E("couldn't open the directory[%s]", dirname);
        }
-
-       _D("finish clean_symlink");
 }
 
 int _app2sd_copy_dir(const char *src, const char *dest)
@@ -203,7 +196,7 @@ int _app2sd_copy_dir(const char *src, const char *dest)
                        return ret;
                } else {
                        _E("failed to open src(%s) dir, errno(%d)", errno);
-                       return APP2EXT_ERROR_MOVE;
+                       return APP2EXT_ERROR_ACCESS_FILE;
                }
        }
 
@@ -213,17 +206,17 @@ int _app2sd_copy_dir(const char *src, const char *dest)
        } else {
                if (errno == ENOENT) {
                        _E("dest(%s) not exist, failed!", dest);
-                       return APP2EXT_ERROR_MOVE;
+                       return APP2EXT_ERROR_ACCESS_FILE;
                } else {
                        _E("failed to open dest(%s) dir, errno(%d)", errno);
-                       return APP2EXT_ERROR_MOVE;
+                       return APP2EXT_ERROR_ACCESS_FILE;
                }
        }
 
        ret = _xsystem(argv_bin);
        if (ret) {
                _E("failed to copy dir, errno(%d)", errno);
-               return APP2EXT_ERROR_MOVE;
+               return APP2EXT_ERROR_ACCESS_FILE;
        }
        return ret;
 }
@@ -235,7 +228,7 @@ int _app2sd_rename_dir(const char *old_name, const char *new_name)
        ret = _xsystem(argv_bin);
        if (ret) {
                _E("mv/rename fail");
-               return APP2EXT_ERROR_MOVE;
+               return APP2EXT_ERROR_ACCESS_FILE;
        }
        return ret;
 }
@@ -244,17 +237,16 @@ unsigned long long _app2sd_calculate_dir_size(char *dirname)
 {
        static unsigned long long total = 0;
        DIR *dp = NULL;
-       struct dirent ep;
-       struct dirent *er = NULL;
+       struct dirent *ep = NULL;
        char abs_filename[FILENAME_MAX] = { 0, };;
 
        dp = opendir(dirname);
        if (dp != NULL) {
-               while (readdir_r(dp, &ep, &er) == 0 && er != NULL) {
+               while ((ep = readdir(dp)) != NULL) {
                        struct stat stFileInfo;
 
                        snprintf(abs_filename, FILENAME_MAX, "%s/%s", dirname,
-                                ep.d_name);
+                                ep->d_name);
 
                        if (stat(abs_filename, &stFileInfo) < 0)
                                perror(abs_filename);
@@ -262,8 +254,8 @@ unsigned long long _app2sd_calculate_dir_size(char *dirname)
                                total += stFileInfo.st_size;
 
                                if (S_ISDIR(stFileInfo.st_mode)) {
-                                       if (strcmp(ep.d_name, ".")
-                                           && strcmp(ep.d_name, "..")) {
+                                       if (strcmp(ep->d_name, ".")
+                                           && strcmp(ep->d_name, "..")) {
                                                _app2sd_calculate_dir_size
                                                    (abs_filename);
                                        }
@@ -578,42 +570,42 @@ char *_app2sd_find_free_device(void)
 * This is a simple password generator
 * return: On success, it will return the password, else NULL.
 */
-char *_app2sd_generate_password(const char *pkgid)
+char *_app2sd_generate_password(void)
 {
-       char passwd[PASSWD_LEN + 1] = { 0, };
-       char *ret_result = NULL;
-       char set[ASCII_PASSWD_CHAR + 1] =
+       char *passwd;
+       static const char charset[ASCII_PASSWD_CHAR + 1] =
                "!\"#$%&()*+,-./0123456789:;<=>?@ABCDE" \
                "FGHIJKLMNOPQRSTUVWXYZ[\\]^_`abcdefghijklmnopqrstuvwxyz{|}~";
-       unsigned char char_1;
-       unsigned char char_2;
-       int i = 0;
-       int appname_len = strlen(pkgid);
-       int j = appname_len;
-       unsigned int seed;
-
-       /* length of the password */
-       ret_result = (char *)malloc(PASSWD_LEN + 1);
-       if (NULL == ret_result) {
+       int i;
+
+       /* include null byte */
+       passwd = (char *)malloc(sizeof(char) * (PASSWD_LEN + 1));
+       if (passwd == NULL) {
                _E("unable to allocate memory");
                return NULL;
        }
-       memset((void *)ret_result, '\0', PASSWD_LEN + 1);
-
-       while (i < PASSWD_LEN) {
-               seed = time(NULL);
-               if (j > 0) j--;
-               char_1 = (rand_r(&seed) + pkgid[j]) % ASCII_PASSWD_CHAR;
-               char_2 = rand_r(&seed) % ASCII_PASSWD_CHAR;
-               passwd[i] = set[char_1];
-               if (j > 0) j--;
-               passwd[i + 1] = set[((pkgid[j]) * 2) % ASCII_PASSWD_CHAR];
-               if (i < PASSWD_LEN - 3)
-                       passwd[i + 2] = set[char_2];
-               i++;
-       }
 
-       memcpy(ret_result, passwd, PASSWD_LEN + 1);
+       for (i = 0; i < PASSWD_LEN; i++)
+               passwd[i] = charset[g_random_int() % ASCII_PASSWD_CHAR];
+       passwd[i] = '\0';
 
-       return ret_result;
+       return passwd;
+}
+
+#ifdef TIZEN_FEATURE_APP2SD_DMCRYPT_ENCRYPTION
+int _app2sd_check_is_luks_device(const char *device_path)
+{
+       int ret = 0;
+       int result = 1; /* default: luks format */
+       const char *argv_bin[] = { "/sbin/cryptsetup", "isLuks", device_path };
+       ret = _xsystem(argv_bin);
+       if (ret < 0)
+               _E("there was errot to check isLuks");
+
+       if (ret == 1) /* legacy format */
+               result = 0;
+
+       _D("ret(%d), result(%d)", ret, result);
+       return result;
 }
+#endif