Fixed the build error using gcc 13
[platform/core/system/libstorage.git] / src / statvfs.c
index dd038af..6e9f841 100644 (file)
@@ -26,6 +26,7 @@
 #include <assert.h>
 #include <mntent.h>
 #include <tzplatform_config.h>
+#include <libsyscommon/ini-parser.h>
 
 #include "log.h"
 #include "common.h"
 #define EXTERNAL_MEMORY_NODE   "sdcard"
 #define STORAGE_CONF_FILE      "/etc/storage/libstorage.conf"
 
-/* it's for 32bit file offset */
-struct statvfs_32 {
-       unsigned long int f_bsize;
-       unsigned long int f_frsize;
-       unsigned long int f_blocks;
-       unsigned long int f_bfree;
-       unsigned long int f_bavail;
-       unsigned long int f_files;
-       unsigned long int f_ffree;
-       unsigned long int f_favail;
-       unsigned long int f_fsid;
-#ifdef _STATVFSBUF_F_UNUSED
-       int __f_unused;
-#endif
-       unsigned long int f_flag;
-       unsigned long int f_namemax;
-       int __f_spare[6];
-};
-
-#define MAX_LINE    128
-#define MAX_SECTION 64
-#define WHITESPACE  " \t"
-#define NEWLINE     "\n\r"
-#define COMMENT     '#'
 
 #define MATCH(a, b)    (!strncmp(a, b, strlen(a)))
 #define SET_CONF(a, b) (a = (b > 0.0 ? b : a))
 
-struct parse_result {
-       char *section;
-       char *name;
-       char *value;
-};
-
 struct storage_config_info {
        double total_size;
        double check_size;
@@ -79,103 +50,6 @@ struct storage_config_info {
 
 static struct storage_config_info storage_info;
 
-static inline char *trim_str(char *s)
-{
-       char *t;
-       /* left trim */
-       s += strspn(s, WHITESPACE);
-
-       /* right trim */
-       for (t = strchr(s, 0); t > s; t--)
-               if (!strchr(WHITESPACE, t[-1]))
-                       break;
-       *t = 0;
-       return s;
-}
-
-static int config_parse(const char *file_name, int cb(struct parse_result *result,
-                       void *user_data), void *user_data)
-{
-       FILE *f = NULL;
-       struct parse_result result;
-       /* use stack for parsing */
-       char line[MAX_LINE];
-       char section[MAX_SECTION];
-       char *start, *end, *name, *value;
-       int lineno = 0, ret = 0;
-
-       if (!file_name || !cb) {
-               ret = -EINVAL;
-               goto error;
-       }
-
-       /* open conf file */
-       f = fopen(file_name, "r");
-       if (!f) {
-               _E("Failed to open file %s", file_name);
-               ret = -EIO;
-               goto error;
-       }
-
-       /* parsing line by line */
-       while (fgets(line, MAX_LINE, f) != NULL) {
-               lineno++;
-
-               start = line;
-               start[strcspn(start, NEWLINE)] = '\0';
-               start = trim_str(start);
-
-               if (*start == COMMENT) {
-                       continue;
-               } else if (*start == '[') {
-                       /* parse section */
-                       end = strchr(start, ']');
-                       if (!end || *end != ']') {
-                               ret = -EBADMSG;
-                               goto error;
-                       }
-
-                       *end = '\0';
-                       strncpy(section, start + 1, sizeof(section));
-                       section[MAX_SECTION-1] = '\0';
-               } else if (*start) {
-                       /* parse name & value */
-                       end = strchr(start, '=');
-                       if (!end || *end != '=') {
-                               ret = -EBADMSG;
-                               goto error;
-                       }
-                       *end = '\0';
-                       name = trim_str(start);
-                       value = trim_str(end + 1);
-                       end = strchr(value, COMMENT);
-                       if (end && *end == COMMENT) {
-                               *end = '\0';
-                               value = trim_str(value);
-                       }
-
-                       result.section = section;
-                       result.name = name;
-                       result.value = value;
-                       /* callback with parse result */
-                       ret = cb(&result, user_data);
-                       if (ret < 0) {
-                               ret = -EBADMSG;
-                               goto error;
-                       }
-               }
-       }
-       _D("Success to load %s", file_name);
-       fclose(f);
-       return 0;
-
-error:
-       if (f)
-               fclose(f);
-       _E("Failed to read %s:%d!", file_name, lineno);
-       return ret;
-}
-
 static int load_config(struct parse_result *result, void *user_data)
 {
        static int check_size = -1;
@@ -206,23 +80,23 @@ static int load_config(struct parse_result *result, void *user_data)
 
 static void storage_config_load(struct storage_config_info *info)
 {
-       int ret;
+       int ret_val;
 
-       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);
+       ret_val = config_parse(STORAGE_CONF_FILE, load_config, info);
+       if (ret_val < 0)
+               _E("Failed to load %s, %d Use default value!", STORAGE_CONF_FILE, ret_val); //LCOV_EXCL_LINE
 }
 
 static int get_memory_size(const char *path, struct statvfs_32 *buf)
 {
        struct statvfs s;
-       int ret;
+       int ret_val;
 
        assert(buf);
 
-       ret = statvfs(path, &s);
-       if (ret)
-               return -errno;
+       ret_val = statvfs(path, &s);
+       if (ret_val)
+               return -errno; //LCOV_EXCL_LINE System Error
 
        memset(buf, 0, sizeof(struct statvfs_32));
 
@@ -241,21 +115,22 @@ 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;
+       struct statvfs_32 temp = { 0, };
        static unsigned long reserved = 0;
-       int ret;
+       int ret_val;
 
        if (!buf) {
                _E("input param error");
-               return -EINVAL;
+               return STORAGE_ERROR_INVALID_PARAMETER;
        }
 
-       ret = get_memory_size(tzplatform_getenv(TZ_SYS_HOME), &temp);
-       if (ret || temp.f_bsize == 0) {
-               _E("fail to get memory size");
-               return -errno;
+       ret_val = get_memory_size(tzplatform_getenv(TZ_SYS_USER), &temp);
+       if (ret_val || temp.f_bsize == 0) {
+               _E("fail to get memory size %d", ret_val); //LCOV_EXCL_LINE
+               return STORAGE_ERROR_OPERATION_FAILED; //LCOV_EXCL_LINE
        }
 
        if (reserved == 0) {
@@ -272,23 +147,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;
+       int ret_val;
 
        if (!buf) {
-               _E("input param error");
-               return -EINVAL;
+               _E("input param error"); //LCOV_EXCL_LINE
+               return STORAGE_ERROR_INVALID_PARAMETER;
        }
 
-       ret = statvfs(tzplatform_getenv(TZ_SYS_HOME), buf);
-       if (ret) {
-               _E("fail to get memory size");
-               return -errno;
+       ret_val = statvfs(tzplatform_getenv(TZ_SYS_USER), buf);
+       if (ret_val) {
+               _E("fail to get memory size"); //LCOV_EXCL_LINE
+               return STORAGE_ERROR_OPERATION_FAILED; //LCOV_EXCL_LINE
        }
 
        if (reserved == 0) {
@@ -303,10 +179,10 @@ 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(char *path)
+int mount_check(const char *path)
 {
        int ret = false;
        struct mntent *mnt;
@@ -331,37 +207,42 @@ 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;
+       int ret_val;
        char ext_path[32];
 
        _D("storage_get_external_memory_size");
        if (!buf) {
                _E("input param error");
-               return -EINVAL;
+               return STORAGE_ERROR_INVALID_PARAMETER;
        }
 
        if (path)
                snprintf(ext_path, sizeof(ext_path), "%s", path);
        else {
-               ret = get_external_path(ext_path, sizeof(ext_path));
-               if (ret == -ENODEV)
+               if (!storage_ext_is_supported()) {
+                       _D("Block module is not enabled");
+                       return STORAGE_ERROR_NOT_SUPPORTED;
+               }
+               ret_val = get_external_path(ext_path, sizeof(ext_path));
+               if (ret_val == -ENODEV)
                        goto out_nodev;
-               if (ret < 0) {
-                       _E("Failed to get external path(%d)", ret);
-                       return ret;
+               if (ret_val < 0) {
+                       _E("Failed to get external path(%d)", ret_val); //LCOV_EXCL_LINE
+                       return STORAGE_ERROR_OPERATION_FAILED; //LCOV_EXCL_LINE
                }
        }
 
-       if (!mount_check(ext_path))
+       if (!mount_check((const char *)ext_path))
                goto out_nodev;
 
-       ret = get_memory_size(ext_path, &temp);
-       if (ret) {
-               _E("fail to get memory size");
-               return -errno;
+       ret_val = storage_ext_get_statvfs(ext_path, &temp);
+       if (ret_val != 0) {
+               _E("fail to get memory size"); //LCOV_EXCL_LINE
+               return STORAGE_ERROR_OPERATION_FAILED; //LCOV_EXCL_LINE
        }
 
        memcpy(buf, &temp, sizeof(temp));
@@ -369,42 +250,49 @@ int storage_get_external_memory_size_with_path(char *path, struct statvfs *buf)
 
 out_nodev:
        memset(buf, 0, sizeof(struct statvfs_32));
-       return 0;
+       return STORAGE_ERROR_NONE;
 }
 
+/* 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;
+       int ret_val;
        char ext_path[32];
 
        _D("storage_get_external_memory_size64");
        if (!buf) {
                _E("input param error");
-               return -EINVAL;
+               return STORAGE_ERROR_INVALID_PARAMETER;
        }
 
        if (path)
                snprintf(ext_path, sizeof(ext_path), "%s", path);
        else {
-               ret = get_external_path(ext_path, sizeof(ext_path));
-               if (ret == -ENODEV)
+               if (!storage_ext_is_supported()) {
+                       _D("Block module is not enabled");
+                       return STORAGE_ERROR_NOT_SUPPORTED;
+               }
+               ret_val = get_external_path(ext_path, sizeof(ext_path));
+               if (ret_val == -ENODEV)
                        goto out_nodev;
-               if (ret < 0) {
-                       _E("Failed to get external path(%d)", ret);
-                       return ret;
+               if (ret_val < 0) {
+                       _E("Failed to get external path(%d)", ret_val);
+                       return STORAGE_ERROR_OPERATION_FAILED; //LCOV_EXCL_LINE
                }
        }
 
-       if (!mount_check(ext_path))
+       if (!mount_check((const char *)ext_path))
                goto out_nodev;
 
-       ret = statvfs(ext_path, buf);
-       if (ret) {
+       ret_val = storage_ext_get_statvfs_size64(ext_path, buf);
+       if (ret_val != 0) {
+       //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 0;
+       return STORAGE_ERROR_NONE;
 
 out_nodev:
        memset(buf, 0, sizeof(struct statvfs));