Fix documentation for storage_get_internal_memory_size and storage_get_external_memor...
[platform/core/system/libstorage.git] / src / statvfs.c
index 81d323f..13c5561 100644 (file)
 #include <errno.h>
 #include <assert.h>
 #include <mntent.h>
+#include <tzplatform_config.h>
 
 #include "log.h"
 #include "common.h"
+#include "storage-external.h"
 
 #define MEMORY_GIGABYTE_VALUE  1073741824
 #define MEMORY_MEGABYTE_VALUE  1048576
 
-#define MEMORY_STATUS_USR_PATH "/opt/usr"
-#define EXTERNAL_MEMORY_PATH   "/opt/storage/sdcard"
+#define EXTERNAL_MEMORY_NODE   "sdcard"
 #define STORAGE_CONF_FILE      "/etc/storage/libstorage.conf"
 
 /* it's for 32bit file offset */
@@ -93,7 +94,7 @@ static inline char *trim_str(char *s)
 }
 
 static int config_parse(const char *file_name, int cb(struct parse_result *result,
-    void *user_data), void *user_data)
+                       void *user_data), void *user_data)
 {
        FILE *f = NULL;
        struct parse_result result;
@@ -111,7 +112,7 @@ static int config_parse(const char *file_name, int cb(struct parse_result *resul
        /* open conf file */
        f = fopen(file_name, "r");
        if (!f) {
-               _E("Failed to open file %s", file_name);
+               _E("Failed to open file %s", file_name); //LCOV_EXCL_LINE
                ret = -EIO;
                goto error;
        }
@@ -171,7 +172,7 @@ static int config_parse(const char *file_name, int cb(struct parse_result *resul
 error:
        if (f)
                fclose(f);
-       _E("Failed to read %s:%d!", file_name, lineno);
+       _E("Failed to read %s:%d!", file_name, lineno); //LCOV_EXCL_LINE
        return ret;
 }
 
@@ -192,7 +193,7 @@ static int load_config(struct parse_result *result, void *user_data)
        value = result->value;
 
        if (info->check_size > 0 && check_size < 0)
-               check_size = (storage_info.total_size < info->check_size)? 1 : 0;
+               check_size = (storage_info.total_size < info->check_size) ? 1 : 0;
        if (MATCH(name, "CHECK_SIZE"))
                info->check_size = atoi(value);
        else if (check_size == 0 && MATCH(name, "RESERVE"))
@@ -209,7 +210,7 @@ static void storage_config_load(struct storage_config_info *info)
 
        ret = config_parse(STORAGE_CONF_FILE, load_config, info);
        if (ret < 0)
-               _E("Failed to load %s, %d Use default value!", STORAGE_CONF_FILE, ret);
+               _E("Failed to load %s, %d Use default value!", STORAGE_CONF_FILE, ret); //LCOV_EXCL_LINE
 }
 
 static int get_memory_size(const char *path, struct statvfs_32 *buf)
@@ -221,7 +222,9 @@ static int get_memory_size(const char *path, struct statvfs_32 *buf)
 
        ret = statvfs(path, &s);
        if (ret)
-               return -errno;
+               return -errno; //LCOV_EXCL_LINE System Error
+
+       memset(buf, 0, sizeof(struct statvfs_32));
 
        buf->f_bsize  = s.f_bsize;
        buf->f_frsize = s.f_frsize;
@@ -238,6 +241,7 @@ static int get_memory_size(const char *path, struct statvfs_32 *buf)
        return 0;
 }
 
+/* This api is intended for binaries built with _FILE_OFFSET_BITS=32 */
 API int storage_get_internal_memory_size(struct statvfs *buf)
 {
        struct statvfs_32 temp;
@@ -246,13 +250,13 @@ API int storage_get_internal_memory_size(struct statvfs *buf)
 
        if (!buf) {
                _E("input param error");
-               return -EINVAL;
+               return STORAGE_ERROR_INVALID_PARAMETER;
        }
 
-       ret = get_memory_size(MEMORY_STATUS_USR_PATH, &temp);
+       ret = get_memory_size(tzplatform_getenv(TZ_SYS_HOME), &temp);
        if (ret || temp.f_bsize == 0) {
-               _E("fail to get memory size");
-               return -errno;
+               _E("fail to get memory size %d", ret); //LCOV_EXCL_LINE
+               return STORAGE_ERROR_OPERATION_FAILED; //LCOV_EXCL_LINE
        }
 
        if (reserved == 0) {
@@ -269,23 +273,24 @@ API int storage_get_internal_memory_size(struct statvfs *buf)
                temp.f_bavail -= reserved;
 
        memcpy(buf, &temp, sizeof(temp));
-       return 0;
+       return STORAGE_ERROR_NONE;
 }
 
+/* This api is intended for binaries built with __USE_FILE_OFFSET64(_FILE_OFFSET_BITS=64) */
 API int storage_get_internal_memory_size64(struct statvfs *buf)
 {
        static unsigned long reserved = 0;
        int ret;
 
        if (!buf) {
-               _E("input param error");
-               return -EINVAL;
+               _E("input param error"); //LCOV_EXCL_LINE
+               return STORAGE_ERROR_INVALID_PARAMETER;
        }
 
-       ret = statvfs(MEMORY_STATUS_USR_PATH, buf);
+       ret = statvfs(tzplatform_getenv(TZ_SYS_HOME), buf);
        if (ret) {
-               _E("fail to get memory size");
-               return -errno;
+               _E("fail to get memory size"); //LCOV_EXCL_LINE
+               return STORAGE_ERROR_OPERATION_FAILED; //LCOV_EXCL_LINE
        }
 
        if (reserved == 0) {
@@ -300,20 +305,20 @@ API int storage_get_internal_memory_size64(struct statvfs *buf)
                buf->f_bavail = 0;
        else
                buf->f_bavail -= reserved;
-       return 0;
+       return STORAGE_ERROR_NONE;
 }
 
-static int mount_check(const char* path)
+static int mount_check(char *path)
 {
        int ret = false;
-       struct mntentmnt;
-       const chartable = "/etc/mtab";
-       FILEfp;
+       struct mntent *mnt;
+       const char *table = "/etc/mtab";
+       FILE *fp;
 
        fp = setmntent(table, "r");
        if (!fp)
                return ret;
-       while ((mnt=getmntent(fp))) {
+       while ((mnt = getmntent(fp))) {
                if (!strcmp(mnt->mnt_dir, path)) {
                        ret = true;
                        break;
@@ -323,52 +328,109 @@ static int mount_check(const char* path)
        return ret;
 }
 
-API int storage_get_external_memory_size(struct statvfs *buf)
+static int get_external_path(char *path, size_t len)
+{
+       return storage_ext_get_primary_mmc_path(path, len);
+}
+
+/* This api is intended for binaries built with _FILE_OFFSET_BITS=32 */
+int storage_get_external_memory_size_with_path(char *path, struct statvfs *buf)
 {
        struct statvfs_32 temp;
        int ret;
+       char ext_path[32];
 
        _D("storage_get_external_memory_size");
        if (!buf) {
                _E("input param error");
-               return -EINVAL;
+               return STORAGE_ERROR_INVALID_PARAMETER;
        }
 
-       if (!mount_check(EXTERNAL_MEMORY_PATH)) {
-               memset(buf, 0, sizeof(struct statvfs_32));
-               return 0;
+       if (path)
+               snprintf(ext_path, sizeof(ext_path), "%s", path);
+       else {
+               if (!storage_ext_is_supported()) {
+                       _D("Block module is not enabled");
+                       goto out_nodev;
+               }
+               ret = get_external_path(ext_path, sizeof(ext_path));
+               if (ret == -ENODEV)
+                       goto out_nodev;
+               if (ret < 0) {
+                       _E("Failed to get external path(%d)", ret); //LCOV_EXCL_LINE
+                       return STORAGE_ERROR_OPERATION_FAILED; //LCOV_EXCL_LINE
+               }
        }
 
-       ret = get_memory_size(EXTERNAL_MEMORY_PATH, &temp);
+       if (!mount_check(ext_path))
+               goto out_nodev;
+
+       ret = get_memory_size(ext_path, &temp);
        if (ret) {
-               _E("fail to get memory size");
-               return -errno;
+               _E("fail to get memory size"); //LCOV_EXCL_LINE
+               return STORAGE_ERROR_OPERATION_FAILED; //LCOV_EXCL_LINE
        }
 
        memcpy(buf, &temp, sizeof(temp));
        return 0;
+
+out_nodev:
+       memset(buf, 0, sizeof(struct statvfs_32));
+       return STORAGE_ERROR_NONE;
 }
 
-API int storage_get_external_memory_size64(struct statvfs *buf)
+/* This api is intended for binaries built with __USE_FILE_OFFSET64(_FILE_OFFSET_BITS=64) */
+int storage_get_external_memory_size64_with_path(char *path, struct statvfs *buf)
 {
        int ret;
+       char ext_path[32];
 
        _D("storage_get_external_memory_size64");
        if (!buf) {
                _E("input param error");
-               return -EINVAL;
+               return STORAGE_ERROR_INVALID_PARAMETER;
        }
 
-       if (!mount_check(EXTERNAL_MEMORY_PATH)) {
-               memset(buf, 0, sizeof(struct statvfs));
-               return 0;
+       if (path)
+               snprintf(ext_path, sizeof(ext_path), "%s", path);
+       else {
+               if (!storage_ext_is_supported()) {
+                       _D("Block module is not enabled");
+                       goto out_nodev;
+               }
+               ret = get_external_path(ext_path, sizeof(ext_path));
+               if (ret == -ENODEV)
+                       goto out_nodev;
+               if (ret < 0) {
+                       _E("Failed to get external path(%d)", ret);
+                       return STORAGE_ERROR_OPERATION_FAILED; //LCOV_EXCL_LINE
+               }
        }
 
-       ret = statvfs(EXTERNAL_MEMORY_PATH, buf);
+       if (!mount_check(ext_path))
+               goto out_nodev;
+
+       ret = statvfs(ext_path, buf);
        if (ret) {
+       //LCOV_EXCL_START System Error
                _E("fail to get memory size");
-               return -errno;
+               return STORAGE_ERROR_OPERATION_FAILED; //LCOV_EXCL_LINE
+       //LCOV_EXCL_STOP
        }
 
+       return STORAGE_ERROR_NONE;
+
+out_nodev:
+       memset(buf, 0, sizeof(struct statvfs));
        return 0;
 }
+
+API int storage_get_external_memory_size(struct statvfs *buf)
+{
+       return storage_get_external_memory_size_with_path(NULL, buf);
+}
+
+API int storage_get_external_memory_size64(struct statvfs *buf)
+{
+       return storage_get_external_memory_size64_with_path(NULL, buf);
+}