From fe84aaecd0ea19e055dbfc1b6f7d671e0da07920 Mon Sep 17 00:00:00 2001 From: Taeyoung Kim Date: Thu, 28 Apr 2016 17:54:52 +0900 Subject: [PATCH] external: add functions to get storage size - The functions to get storage size of given path are added. - For the following apis, default path is set to primary sdcard. int storage_get_external_memory_size(struct statvfs *buf) int storage_get_external_memory_size64(struct statvfs *buf) Change-Id: I0cfdcf2496849520cf0ef00a3ee2c3a70ec3c06c Signed-off-by: Taeyoung Kim --- include/common.h | 4 +++ src/statvfs.c | 52 +++++++++++++++++++++++++------ src/storage-external.c | 84 ++++++++++++++++++++++++++++++++++++++++++++++++++ src/storage-external.h | 3 ++ src/storage.c | 4 +-- 5 files changed, 135 insertions(+), 12 deletions(-) diff --git a/include/common.h b/include/common.h index 7ff5ce9..ec9ca67 100644 --- a/include/common.h +++ b/include/common.h @@ -24,6 +24,7 @@ extern "C" { #endif #include "storage-expand.h" +#include #ifndef API #define API __attribute__ ((visibility("default"))) @@ -75,6 +76,9 @@ static void __DESTRUCTOR__ module_exit(void) \ void add_device(const struct storage_ops *st); void remove_device(const struct storage_ops *st); +int storage_get_external_memory_size_with_path(char *path, struct statvfs *buf); +int storage_get_external_memory_size64_with_path(char *path, struct statvfs *buf); + #ifdef __cplusplus } #endif diff --git a/src/statvfs.c b/src/statvfs.c index 52e030c..6ccdb5e 100644 --- a/src/statvfs.c +++ b/src/statvfs.c @@ -29,6 +29,7 @@ #include "log.h" #include "common.h" +#include "storage-external.h" #define MEMORY_GIGABYTE_VALUE 1073741824 #define MEMORY_MEGABYTE_VALUE 1048576 @@ -305,7 +306,7 @@ API int storage_get_internal_memory_size64(struct statvfs *buf) return 0; } -static int mount_check(const char *path) +static int mount_check(char *path) { int ret = false; struct mntent *mnt; @@ -325,16 +326,16 @@ static int mount_check(const char *path) return ret; } -static const char *get_external_path(void) +static int get_external_path(char *path, size_t len) { - return tzplatform_mkpath(TZ_SYS_MEDIA, - EXTERNAL_MEMORY_NODE); + return storage_ext_get_primary_mmc_path(path, len); } -API int storage_get_external_memory_size(struct statvfs *buf) +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) { @@ -342,12 +343,22 @@ API int storage_get_external_memory_size(struct statvfs *buf) return -EINVAL; } - if (!mount_check(get_external_path())) { + if (path) + snprintf(ext_path, sizeof(ext_path), "%s", path); + else { + ret = get_external_path(ext_path, sizeof(ext_path)); + if (ret < 0) { + _E("Failed to get external path(%d)", ret); + return ret; + } + } + + if (!mount_check(ext_path)) { memset(buf, 0, sizeof(struct statvfs_32)); return 0; } - ret = get_memory_size(get_external_path(), &temp); + ret = get_memory_size(ext_path, &temp); if (ret) { _E("fail to get memory size"); return -errno; @@ -357,9 +368,10 @@ API int storage_get_external_memory_size(struct statvfs *buf) return 0; } -API int storage_get_external_memory_size64(struct statvfs *buf) +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) { @@ -367,12 +379,22 @@ API int storage_get_external_memory_size64(struct statvfs *buf) return -EINVAL; } - if (!mount_check(get_external_path())) { + if (path) + snprintf(ext_path, sizeof(ext_path), "%s", path); + else { + ret = get_external_path(ext_path, sizeof(ext_path)); + if (ret < 0) { + _E("Failed to get external path(%d)", ret); + return ret; + } + } + + if (!mount_check(ext_path)) { memset(buf, 0, sizeof(struct statvfs)); return 0; } - ret = statvfs(get_external_path(), buf); + ret = statvfs(ext_path, buf); if (ret) { _E("fail to get memory size"); return -errno; @@ -380,3 +402,13 @@ API int storage_get_external_memory_size64(struct statvfs *buf) 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); +} diff --git a/src/storage-external.c b/src/storage-external.c index 8fdde8c..d6d6bca 100755 --- a/src/storage-external.c +++ b/src/storage-external.c @@ -60,6 +60,62 @@ static int storage_ext_get_dev_state(storage_ext_device *dev, } } +int storage_ext_get_space(int storage_id, + unsigned long long *total, unsigned long long *available) +{ + storage_state_e state; + struct statvfs s; + int ret; + unsigned long long t = 0, a = 0; + storage_ext_device *dev; + + if (storage_id < 0) + return -EINVAL; + + dev = calloc(1, sizeof(storage_ext_device)); + if (!dev) { + _E("calloc failed"); + return -ENOMEM; + } + + ret = storage_ext_get_device_info(storage_id, dev); + if (ret < 0) { + _E("Cannot get the storage with id (%d, ret:%d)", storage_id, ret); + goto out; + } + + ret = storage_ext_get_dev_state(dev, STORAGE_EXT_CHANGED, &state); + if (ret < 0) { + _E("Failed to get state of storage (id:%d, ret:%d)", storage_id, ret); + goto out; + } + + if (state >= STORAGE_STATE_MOUNTED) { +#ifndef __USE_FILE_OFFSET64 + ret = storage_get_external_memory_size_with_path(dev->mount_point, &s); +#else + ret = storage_get_external_memory_size64_with_path(dev->mount_point, &s); +#endif + if (ret < 0) { + _E("Failed to get external memory size of (%s)(ret:%d)", dev->mount_point, ret); + goto out; + } + + t = (unsigned long long)s.f_frsize*s.f_blocks; + a = (unsigned long long)s.f_bsize*s.f_bavail; + } + + if (total) + *total = t; + if (available) + *available = a; + + ret = 0; +out: + storage_ext_release_device(&dev); + return ret; +} + int storage_ext_foreach_device_list(storage_device_supported_cb callback, void *user_data) { int ret; @@ -249,3 +305,31 @@ out: storage_ext_release_device(&dev); return ret; } + +int storage_ext_get_primary_mmc_path(char *path, size_t len) +{ + dd_list *list = NULL, *elem; + storage_ext_device *dev; + int ret; + + ret = storage_ext_get_list(&list); + if (ret < 0) { + _E("Failed to get external storage list from deviced (%d)", errno); + return ret; + } + + DD_LIST_FOREACH(list, elem, dev) { + if (dev->primary) { + snprintf(path, len, "%s", dev->mount_point); + ret = 0; + goto out; + } + } + + ret = -ENODEV; + +out: + if (list) + storage_ext_release_list(&list); + return ret; +} diff --git a/src/storage-external.h b/src/storage-external.h index a9046af..c7bd9fd 100755 --- a/src/storage-external.h +++ b/src/storage-external.h @@ -21,10 +21,13 @@ #include "common.h" #include "storage-external-dbus.h" +int storage_ext_get_space(int storage_id, + unsigned long long *total, unsigned long long *available); int storage_ext_foreach_device_list(storage_device_supported_cb callback, void *user_data); int storage_ext_register_cb(enum storage_cb_type type, struct storage_cb_info *info); int storage_ext_unregister_cb(enum storage_cb_type type, struct storage_cb_info *info); int storage_ext_get_root(int storage_id, char *path, size_t len); int storage_ext_get_state(int storage_id, storage_state_e *state); +int storage_ext_get_primary_mmc_path(char *path, size_t len); #endif /* __STORAGE_EXTERNAL_H__ */ diff --git a/src/storage.c b/src/storage.c index 41a3b3b..c71af5c 100644 --- a/src/storage.c +++ b/src/storage.c @@ -327,7 +327,7 @@ API int storage_get_total_space(int storage_id, unsigned long long *bytes) } /* external storage */ - ret = 0; + ret = storage_ext_get_space(storage_id, &total, NULL); out: if (ret < 0) { @@ -360,7 +360,7 @@ API int storage_get_available_space(int storage_id, unsigned long long *bytes) } /* external storage */ - ret = 0; + ret = storage_ext_get_space(storage_id,NULL, &avail); out: if (ret < 0) { -- 2.7.4