From e2ce4b26cd01e0e3a96fcbe1d7f05fab4b91505d Mon Sep 17 00:00:00 2001 From: "pr.jung" Date: Tue, 8 May 2018 16:30:50 +0900 Subject: [PATCH 01/16] When storage id is invalid GetDeviceInfo return with id which is less than 0 Change-Id: I851d7d457c2e341a753adb4dfc951a150c4fee1e Signed-off-by: pr.jung --- src/storage-external-dbus.c | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/src/storage-external-dbus.c b/src/storage-external-dbus.c index 4568371..8b66ca3 100755 --- a/src/storage-external-dbus.c +++ b/src/storage-external-dbus.c @@ -493,6 +493,11 @@ int storage_ext_get_device_info(int storage_id, storage_ext_device *info) return -ENODEV; } + if (info->storage_id < 0) { + _E("No storage with the storage id (%d)", storage_id); //LCOV_EXCL_LINE + return -ENODEV; + } + g_variant_unref(result); return 0; -- 2.7.4 From 6e5c758d6678b5906952d6ef253ed9107ba61b49 Mon Sep 17 00:00:00 2001 From: "pr.jung" Date: Tue, 12 Jun 2018 11:30:07 +0900 Subject: [PATCH 02/16] Make a new public api: storage_get_type_dev Change-Id: I318bd964d15b61bc0627b536e4a101ba27b4528a Signed-off-by: pr.jung --- CMakeLists.txt | 3 +- include/storage-expand.h | 24 ++++++++++++++++ include/storage-experimental.h | 62 ------------------------------------------ src/storage-inhouse.c | 55 ------------------------------------- src/storage.c | 45 ++++++++++++++++++++++++++++++ 5 files changed, 70 insertions(+), 119 deletions(-) delete mode 100644 include/storage-experimental.h diff --git a/CMakeLists.txt b/CMakeLists.txt index 20f7f7e..3169d78 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -31,8 +31,7 @@ ENDFOREACH(flag) SET(HEADERS include/storage.h include/storage-expand.h - include/storage-internal.h - include/storage-experimental.h) + include/storage-internal.h) SET(EXTRA_CFLAGS "${EXTRA_CFLAGS} -fvisibility=hidden") SET(EXTRA_CFLAGS "${EXTRA_CFLAGS} -g") diff --git a/include/storage-expand.h b/include/storage-expand.h index 5db5a9b..d5c0843 100644 --- a/include/storage-expand.h +++ b/include/storage-expand.h @@ -349,6 +349,30 @@ int storage_get_available_space(int storage_id, unsigned long long *bytes); /** + * @brief Gets the type and the kind of external device for the given storage id. + * + * @since_tizen 5.0 + * + * @remarks This function works only for external storages. + * If @a type is #STORAGE_TYPE_INTERNAL, this function returns #STORAGE_ERROR_INVALID_PARAMETER and @a dev is unchanged. + * + * @param[in] storage_id The storage id + * @param[out] type The storage @a type (internal or external). If @a type is #STORAGE_TYPE_INTERNAL, this function returns #STORAGE_ERROR_INVALID_PARAMETER and @a dev is unchanged. + * @param[out] dev The storage device for external storage. + * + * @return @c 0 on success, + * otherwise a negative error value + * + * @retval #STORAGE_ERROR_NONE Successful + * @retval #STORAGE_ERROR_INVALID_PARAMETER Invalid parameter + * @retval #STORAGE_ERROR_OUT_OF_MEMORY Out of memory + * @retval #STORAGE_ERROR_NOT_SUPPORTED Storage not supported + * @retval #STORAGE_ERROR_OPERATION_FAILED Operation failed + */ +int storage_get_type_dev(int storage_id, storage_type_e *type, storage_dev_e *dev); + + +/** * @} */ diff --git a/include/storage-experimental.h b/include/storage-experimental.h deleted file mode 100644 index 5f547be..0000000 --- a/include/storage-experimental.h +++ /dev/null @@ -1,62 +0,0 @@ -/* - * storage - * Copyright (c) 2016 Samsung Electronics Co., Ltd. - * - * Licensed under the Apache License, Version 2.0 (the License); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - - -#ifndef __STORAGE_EXPERIMENTAL_H__ -#define __STORAGE_EXPERIMENTAL_H__ - -#ifdef __cplusplus -extern "C" { -#endif - - -/** - * @addtogroup CAPI_SYSTEM_STORAGE_MODULE - * @{ - */ - -#include - -#define STORAGE_ERROR_NO_DEVICE TIZEN_ERROR_NO_SUCH_DEVICE - -/** - * @brief Get the type and the kind of external device for given storage id. - * - * @since_tizen 3.0 - * - * @param[in] storage_id The storage id - * @param[out] type The storage @a type (internal or external). - * @param[out] dev The storage device for external storage. If @a type is #STORAGE_TYPE_INTERNAL, then value of @a dev should be ignored. - * - * @return @c 0 on success, - * otherwise a negative error value - * - * @retval #STORAGE_ERROR_NONE Successful - * @retval #STORAGE_ERROR_INVALID_PARAMETER Invalid parameter - * @retval #STORAGE_ERROR_OUT_OF_MEMORY Out of memory - * @retval #STORAGE_ERROR_NO_DEVICE No such device - */ -int storage_get_type_dev(int storage_id, storage_type_e *type, storage_dev_e *dev); - -/** - * @} - */ - -#ifdef __cplusplus -} -#endif -#endif /* __STORAGE_EXPERIMENTAL_H__ */ diff --git a/src/storage-inhouse.c b/src/storage-inhouse.c index a736890..34ec31d 100755 --- a/src/storage-inhouse.c +++ b/src/storage-inhouse.c @@ -26,7 +26,6 @@ #include "log.h" #include "storage-internal.h" #include "storage-external-dbus.h" -#include "storage-experimental.h" /* Get compat path from origin Multi-user path @@ -182,57 +181,3 @@ API int storage_get_primary_sdcard(int *storage_id, char **path) return STORAGE_ERROR_NONE; } -API int storage_get_type_dev(int storage_id, storage_type_e *type, storage_dev_e *dev) -{ - storage_ext_device *ext_dev; - int ret; - - if (storage_id < 0) { - _E("Invalid parameger"); - return STORAGE_ERROR_NO_DEVICE; - } - - if (!type) { - _E("Invalid parameger"); - return STORAGE_ERROR_INVALID_PARAMETER; - } - - if (!dev) { - _E("Invalid parameger"); - return STORAGE_ERROR_INVALID_PARAMETER; - } - - ret = storage_get_type(storage_id, type); - if (ret != STORAGE_ERROR_NONE) { - _E("Failed to get storage type: %d", ret); - return ret; - } - if (*type == STORAGE_TYPE_INTERNAL) - return STORAGE_ERROR_NONE; - - ext_dev = calloc(1, sizeof(storage_ext_device)); - if (!ext_dev) { - //LCOV_EXCL_START System Error - _E("calloc failed"); - return STORAGE_ERROR_OUT_OF_MEMORY; - //LCOV_EXCL_STOP - } - - ret = storage_ext_get_device_info(storage_id, ext_dev); - if (ret < 0) { - _E("Cannot get the storage with id (%d, ret:%d)", storage_id, ret); //LCOV_EXCL_LINE - ret = STORAGE_ERROR_NO_DEVICE; - goto out; - } - - if (ext_dev->type == STORAGE_EXT_SCSI) - *dev = STORAGE_DEV_EXT_USB_MASS_STORAGE; - else if (ext_dev->type == STORAGE_EXT_MMC) - *dev = STORAGE_DEV_EXT_SDCARD; - ret = STORAGE_ERROR_NONE; - _I("type: %d(internal:0, external:1) dev: %d(sdcard: 1001, usb: 1002)", *type, *dev); - -out: - storage_ext_release_device(&ext_dev); - return ret; -} diff --git a/src/storage.c b/src/storage.c index d79a00d..9a45507 100644 --- a/src/storage.c +++ b/src/storage.c @@ -603,6 +603,51 @@ API int storage_unset_changed_cb(storage_type_e type, storage_changed_cb callbac return STORAGE_ERROR_NONE; } +API int storage_get_type_dev(int storage_id, storage_type_e *type, storage_dev_e *dev) +{ + storage_ext_device *ext_dev; + int ret; + + if (storage_id < 0 || !type || !dev) { + _E("Invalid parameter"); + return STORAGE_ERROR_INVALID_PARAMETER; + } + + ret = storage_get_type(storage_id, type); + if (ret != STORAGE_ERROR_NONE) { + _E("Failed to get storage type: %d", ret); + return ret; + } + if (*type == STORAGE_TYPE_INTERNAL || *type == STORAGE_TYPE_EXTENDED_INTERNAL) + return STORAGE_ERROR_INVALID_PARAMETER; + + ext_dev = calloc(1, sizeof(storage_ext_device)); + if (!ext_dev) { + //LCOV_EXCL_START System Error + _E("calloc failed"); + return STORAGE_ERROR_OUT_OF_MEMORY; + //LCOV_EXCL_STOP + } + + ret = storage_ext_get_device_info(storage_id, ext_dev); + if (ret < 0) { + _E("Cannot get the storage with id (%d, ret:%d)", storage_id, ret); //LCOV_EXCL_LINE + ret = STORAGE_ERROR_OPERATION_FAILED; + goto out; + } + + if (ext_dev->type == STORAGE_EXT_SCSI) + *dev = STORAGE_DEV_EXT_USB_MASS_STORAGE; + else if (ext_dev->type == STORAGE_EXT_MMC) + *dev = STORAGE_DEV_EXT_SDCARD; + ret = STORAGE_ERROR_NONE; + _I("type: %d(internal:0, external:1) dev: %d(sdcard: 1001, usb: 1002)", *type, *dev); + +out: + storage_ext_release_device(&ext_dev); + return ret; +} + static void __CONSTRUCTOR__ init(void) { const char *tmp; -- 2.7.4 From 77ea1cf801171f2ba5fab03fdc9f41ab01bec54c Mon Sep 17 00:00:00 2001 From: "pr.jung" Date: Wed, 20 Jun 2018 14:56:52 +0900 Subject: [PATCH 03/16] Add new internal api - storage_get_storage_level - Get the current storage level Change-Id: I9be56c1c98a38cc210582aee1072b0b9d340badd Signed-off-by: pr.jung --- include/storage-internal.h | 2 ++ src/storage-external-dbus.c | 36 ++++++++++++++++++---- src/storage-external-dbus.h | 2 ++ src/storage-inhouse.c | 74 ++++++++++++++++++++++++++++++++++++++++++++- 4 files changed, 107 insertions(+), 7 deletions(-) diff --git a/include/storage-internal.h b/include/storage-internal.h index 391a721..1b14798 100644 --- a/include/storage-internal.h +++ b/include/storage-internal.h @@ -30,6 +30,7 @@ extern "C" { */ #include +#include #include "storage.h" #define STORAGE_ERROR_NO_DEVICE TIZEN_ERROR_NO_SUCH_DEVICE @@ -55,6 +56,7 @@ int storage_get_primary_sdcard(int *storage_id, char **path); int storage_get_compat_internal_path(const char* origin, int len, char* compat); int storage_get_origin_internal_path(const char* compat, int len, char* origin); +int storage_get_storage_level(enum tzplatform_variable id, char **level); /** * @} diff --git a/src/storage-external-dbus.c b/src/storage-external-dbus.c index 8b66ca3..d335ca7 100755 --- a/src/storage-external-dbus.c +++ b/src/storage-external-dbus.c @@ -33,13 +33,14 @@ #define CHECK_STR(a) (a ? a : "") -#define STORAGE_EXT_GET_LIST "GetDeviceList" -#define STORAGE_EXT_GET_STATVFS "GetStatvfs" +#define STORAGE_EXT_GET_LIST "GetDeviceList" +#define STORAGE_EXT_GET_STATVFS "GetStatvfs" +#define STORAGE_EXT_GET_STORAGE_LEVEL "GetStorageLevel" -#define STORAGE_EXT_DEVICE_CHANGED "DeviceChanged" -#define STORAGE_EXT_DEVICE_ADDED "DeviceAdded" -#define STORAGE_EXT_DEVICE_REMOVED "DeviceRemoved" -#define STORAGE_EXT_DEVICE_BLOCKED "DeviceBlocked" +#define STORAGE_EXT_DEVICE_CHANGED "DeviceChanged" +#define STORAGE_EXT_DEVICE_ADDED "DeviceAdded" +#define STORAGE_EXT_DEVICE_REMOVED "DeviceRemoved" +#define STORAGE_EXT_DEVICE_BLOCKED "DeviceBlocked" #define DBUS_REPLY_TIMEOUT (-1) @@ -286,6 +287,29 @@ int storage_ext_get_statvfs_size64(char *path, struct statvfs *buf) return 0; } +int storage_ext_get_storage_level(enum tzplatform_variable id, char **level) +{ + GVariant *result; + char *tmp; + + result = dbus_method_call_sync(STORAGE_EXT_BUS_NAME, + STORAGE_EXT_PATH_STORAGE, + STORAGE_EXT_IFACE_STORAGE, + STORAGE_EXT_GET_STORAGE_LEVEL, + g_variant_new("(i)", id)); + if (!result) { + _E("Failed to get %d level", id); + return -EIO; + } + + g_variant_get(result, "(s)", &tmp); + *level = strdup(tmp); + if (*level == NULL) + return -ENOMEM; + + return 0; +} + //LCOV_EXCL_START Not called Callback static void storage_ext_device_changed(GVariant *params, enum storage_ext_state state, gpointer user_data) { diff --git a/src/storage-external-dbus.h b/src/storage-external-dbus.h index 195d72e..24800f8 100644 --- a/src/storage-external-dbus.h +++ b/src/storage-external-dbus.h @@ -23,6 +23,7 @@ #include #include #include +#include #include "list.h" #define STORAGE_EXT_BUS_NAME "org.tizen.system.storage" @@ -106,6 +107,7 @@ int storage_ext_register_device_change(storage_ext_changed_cb func, void *data); void storage_ext_unregister_device_change(storage_ext_changed_cb func); int storage_ext_get_device_info(int storage_id, storage_ext_device *info); +int storage_ext_get_storage_level(enum tzplatform_variable id, char **level); /* storage-internal.c */ GVariant *dbus_method_call_sync(const gchar *dest, const gchar *path, diff --git a/src/storage-inhouse.c b/src/storage-inhouse.c index 34ec31d..a522ee9 100755 --- a/src/storage-inhouse.c +++ b/src/storage-inhouse.c @@ -19,7 +19,6 @@ #include #include #include -#include #include "common.h" #include "list.h" @@ -181,3 +180,76 @@ API int storage_get_primary_sdcard(int *storage_id, char **path) return STORAGE_ERROR_NONE; } +API int storage_get_type_dev(int storage_id, storage_type_e *type, storage_dev_e *dev) +{ + storage_ext_device *ext_dev; + int ret; + + if (storage_id < 0) { + _E("Invalid parameger"); + return STORAGE_ERROR_NO_DEVICE; + } + + if (!type) { + _E("Invalid parameger"); + return STORAGE_ERROR_INVALID_PARAMETER; + } + + if (!dev) { + _E("Invalid parameger"); + return STORAGE_ERROR_INVALID_PARAMETER; + } + + ret = storage_get_type(storage_id, type); + if (ret != STORAGE_ERROR_NONE) { + _E("Failed to get storage type: %d", ret); + return ret; + } + if (*type == STORAGE_TYPE_INTERNAL) + return STORAGE_ERROR_NONE; + + ext_dev = calloc(1, sizeof(storage_ext_device)); + if (!ext_dev) { + //LCOV_EXCL_START System Error + _E("calloc failed"); + return STORAGE_ERROR_OUT_OF_MEMORY; + //LCOV_EXCL_STOP + } + + ret = storage_ext_get_device_info(storage_id, ext_dev); + if (ret < 0) { + _E("Cannot get the storage with id (%d, ret:%d)", storage_id, ret); //LCOV_EXCL_LINE + ret = STORAGE_ERROR_NO_DEVICE; + goto out; + } + + if (ext_dev->type == STORAGE_EXT_SCSI) + *dev = STORAGE_DEV_EXT_USB_MASS_STORAGE; + else if (ext_dev->type == STORAGE_EXT_MMC) + *dev = STORAGE_DEV_EXT_SDCARD; + ret = STORAGE_ERROR_NONE; + _I("type: %d(internal:0, external:1) dev: %d(sdcard: 1001, usb: 1002)", *type, *dev); + +out: + storage_ext_release_device(&ext_dev); + return ret; +} + +API int storage_get_storage_level(enum tzplatform_variable id, char **level) +{ + int ret; + + if (!level) + return STORAGE_ERROR_INVALID_PARAMETER; + + if (id != TZ_SYS_USER || id != TZ_SYS_TMP || id != TZ_SYS_OPT) + return STORAGE_ERROR_INVALID_PARAMETER; + + ret = storage_ext_get_storage_level(id, level); + if (ret == -ENOMEM) + return STORAGE_ERROR_OUT_OF_MEMORY; //LCOV_EXCL_LINE System Error + else if (ret < 0) + return STORAGE_ERROR_OPERATION_FAILED; + + return STORAGE_ERROR_NONE; +} -- 2.7.4 From 93e5212a3c6ab4877f57b92a2e71dad432644b2d Mon Sep 17 00:00:00 2001 From: "pr.jung" Date: Mon, 25 Jun 2018 17:57:12 +0900 Subject: [PATCH 04/16] Remove unused code Change-Id: I64ac16874dcbfda72f88ad9ed001e67b064083b3 Signed-off-by: pr.jung --- src/storage-inhouse.c | 55 --------------------------------------------------- 1 file changed, 55 deletions(-) diff --git a/src/storage-inhouse.c b/src/storage-inhouse.c index a522ee9..d3e30d3 100755 --- a/src/storage-inhouse.c +++ b/src/storage-inhouse.c @@ -180,61 +180,6 @@ API int storage_get_primary_sdcard(int *storage_id, char **path) return STORAGE_ERROR_NONE; } -API int storage_get_type_dev(int storage_id, storage_type_e *type, storage_dev_e *dev) -{ - storage_ext_device *ext_dev; - int ret; - - if (storage_id < 0) { - _E("Invalid parameger"); - return STORAGE_ERROR_NO_DEVICE; - } - - if (!type) { - _E("Invalid parameger"); - return STORAGE_ERROR_INVALID_PARAMETER; - } - - if (!dev) { - _E("Invalid parameger"); - return STORAGE_ERROR_INVALID_PARAMETER; - } - - ret = storage_get_type(storage_id, type); - if (ret != STORAGE_ERROR_NONE) { - _E("Failed to get storage type: %d", ret); - return ret; - } - if (*type == STORAGE_TYPE_INTERNAL) - return STORAGE_ERROR_NONE; - - ext_dev = calloc(1, sizeof(storage_ext_device)); - if (!ext_dev) { - //LCOV_EXCL_START System Error - _E("calloc failed"); - return STORAGE_ERROR_OUT_OF_MEMORY; - //LCOV_EXCL_STOP - } - - ret = storage_ext_get_device_info(storage_id, ext_dev); - if (ret < 0) { - _E("Cannot get the storage with id (%d, ret:%d)", storage_id, ret); //LCOV_EXCL_LINE - ret = STORAGE_ERROR_NO_DEVICE; - goto out; - } - - if (ext_dev->type == STORAGE_EXT_SCSI) - *dev = STORAGE_DEV_EXT_USB_MASS_STORAGE; - else if (ext_dev->type == STORAGE_EXT_MMC) - *dev = STORAGE_DEV_EXT_SDCARD; - ret = STORAGE_ERROR_NONE; - _I("type: %d(internal:0, external:1) dev: %d(sdcard: 1001, usb: 1002)", *type, *dev); - -out: - storage_ext_release_device(&ext_dev); - return ret; -} - API int storage_get_storage_level(enum tzplatform_variable id, char **level) { int ret; -- 2.7.4 From f56285beeafc8ec250f04fc6e57559c41baef266 Mon Sep 17 00:00:00 2001 From: "pr.jung" Date: Wed, 27 Jun 2018 14:49:14 +0900 Subject: [PATCH 05/16] Change TZ_SYS_HOME to TZ_SYS_USER - TZ_SYS_HOME : "/opt/usr/home" - TZ_SYS_USER: "/opt/usr" Change-Id: I67e1c179d31559d79dc2ff0edf4f8db0f6c82580 Signed-off-by: pr.jung --- src/statvfs.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/statvfs.c b/src/statvfs.c index e920cb6..99ed755 100644 --- a/src/statvfs.c +++ b/src/statvfs.c @@ -234,7 +234,7 @@ API int storage_get_internal_memory_size(struct statvfs *buf) return -EINVAL; } - ret = get_memory_size(tzplatform_getenv(TZ_SYS_HOME), &temp); + ret = get_memory_size(tzplatform_getenv(TZ_SYS_USER), &temp); if (ret || temp.f_bsize == 0) { _E("fail to get memory size %d", ret); //LCOV_EXCL_LINE return -errno; //LCOV_EXCL_LINE System Error @@ -267,7 +267,7 @@ API int storage_get_internal_memory_size64(struct statvfs *buf) return -EINVAL; } - ret = statvfs(tzplatform_getenv(TZ_SYS_HOME), buf); + ret = statvfs(tzplatform_getenv(TZ_SYS_USER), buf); if (ret) { _E("fail to get memory size"); //LCOV_EXCL_LINE return -errno; //LCOV_EXCL_LINE System Error -- 2.7.4 From a61e011e03a39091e6f66b21a00f1d9d25e43db6 Mon Sep 17 00:00:00 2001 From: Hyotaek Shim Date: Thu, 28 Jun 2018 15:00:31 +0900 Subject: [PATCH 06/16] Add .gitignore Change-Id: I71ff3c054b7ac9f8c006841ef683e79decee0796 Signed-off-by: Hyotaek Shim --- .gitignore | 3 +++ 1 file changed, 3 insertions(+) create mode 100644 .gitignore diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..9306ae6 --- /dev/null +++ b/.gitignore @@ -0,0 +1,3 @@ +cscope.files +cscope.out +tags -- 2.7.4 From ebe8d117ef1e8c593ecd1db932a2931d8aba7b50 Mon Sep 17 00:00:00 2001 From: "pr.jung" Date: Thu, 28 Jun 2018 16:27:54 +0900 Subject: [PATCH 07/16] Modify storage_get_storage_level - Change fist parameter of storage_get_storage_level enum tzplatform_variable -> char * - Remove tzplatform_config.h in storage-internal.h Change-Id: I3f5ac4d6ff47aad5cccce101c7775cb1ce3e0379 Signed-off-by: pr.jung --- include/storage-internal.h | 3 +-- src/storage-external-dbus.c | 15 ++++++++++++++- src/storage-external-dbus.h | 3 +-- src/storage-inhouse.c | 12 ++++++------ 4 files changed, 22 insertions(+), 11 deletions(-) diff --git a/include/storage-internal.h b/include/storage-internal.h index 1b14798..53795d2 100644 --- a/include/storage-internal.h +++ b/include/storage-internal.h @@ -30,7 +30,6 @@ extern "C" { */ #include -#include #include "storage.h" #define STORAGE_ERROR_NO_DEVICE TIZEN_ERROR_NO_SUCH_DEVICE @@ -56,7 +55,7 @@ int storage_get_primary_sdcard(int *storage_id, char **path); int storage_get_compat_internal_path(const char* origin, int len, char* compat); int storage_get_origin_internal_path(const char* compat, int len, char* origin); -int storage_get_storage_level(enum tzplatform_variable id, char **level); +int storage_get_storage_level(const char *path, char **level); /** * @} diff --git a/src/storage-external-dbus.c b/src/storage-external-dbus.c index d335ca7..f070e8f 100755 --- a/src/storage-external-dbus.c +++ b/src/storage-external-dbus.c @@ -27,6 +27,7 @@ #include #include #include +#include #include "log.h" #include "storage-external-dbus.h" @@ -287,10 +288,22 @@ int storage_ext_get_statvfs_size64(char *path, struct statvfs *buf) return 0; } -int storage_ext_get_storage_level(enum tzplatform_variable id, char **level) +int storage_ext_get_storage_level(const char *path, char **level) { GVariant *result; char *tmp; + enum tzplatform_variable id; + + if (!strcmp(path, tzplatform_getenv(TZ_SYS_USER))) + id = TZ_SYS_USER; + else if (!strcmp(path, tzplatform_getenv(TZ_SYS_TMP))) + id = TZ_SYS_TMP; + else if (!strcmp(path, tzplatform_getenv(TZ_SYS_OPT))) + id = TZ_SYS_OPT; + else { + _E("Invalid path"); + return -EINVAL; + } result = dbus_method_call_sync(STORAGE_EXT_BUS_NAME, STORAGE_EXT_PATH_STORAGE, diff --git a/src/storage-external-dbus.h b/src/storage-external-dbus.h index 24800f8..b71d2e4 100644 --- a/src/storage-external-dbus.h +++ b/src/storage-external-dbus.h @@ -23,7 +23,6 @@ #include #include #include -#include #include "list.h" #define STORAGE_EXT_BUS_NAME "org.tizen.system.storage" @@ -107,7 +106,7 @@ int storage_ext_register_device_change(storage_ext_changed_cb func, void *data); void storage_ext_unregister_device_change(storage_ext_changed_cb func); int storage_ext_get_device_info(int storage_id, storage_ext_device *info); -int storage_ext_get_storage_level(enum tzplatform_variable id, char **level); +int storage_ext_get_storage_level(const char *path, char **level); /* storage-internal.c */ GVariant *dbus_method_call_sync(const gchar *dest, const gchar *path, diff --git a/src/storage-inhouse.c b/src/storage-inhouse.c index d3e30d3..051b6b9 100755 --- a/src/storage-inhouse.c +++ b/src/storage-inhouse.c @@ -19,6 +19,7 @@ #include #include #include +#include #include "common.h" #include "list.h" @@ -180,19 +181,18 @@ API int storage_get_primary_sdcard(int *storage_id, char **path) return STORAGE_ERROR_NONE; } -API int storage_get_storage_level(enum tzplatform_variable id, char **level) +API int storage_get_storage_level(const char *path, char **level) { int ret; - if (!level) + if (!level || !path) return STORAGE_ERROR_INVALID_PARAMETER; - if (id != TZ_SYS_USER || id != TZ_SYS_TMP || id != TZ_SYS_OPT) - return STORAGE_ERROR_INVALID_PARAMETER; - - ret = storage_ext_get_storage_level(id, level); + ret = storage_ext_get_storage_level(path, level); if (ret == -ENOMEM) return STORAGE_ERROR_OUT_OF_MEMORY; //LCOV_EXCL_LINE System Error + else if (ret == -EINVAL) + return STORAGE_ERROR_INVALID_PARAMETER; else if (ret < 0) return STORAGE_ERROR_OPERATION_FAILED; -- 2.7.4 From 4306eaee52ee3fcd853fb2b9f08ce69c8e8e7093 Mon Sep 17 00:00:00 2001 From: "pr.jung" Date: Tue, 3 Jul 2018 11:44:20 +0900 Subject: [PATCH 08/16] Add internal apis to mount/unmount/format primary sdcard - storage_request_mount_mmc - storage_request_unmount_mmc - storage_request_format_mmc - storage_format_mmc Change-Id: I48bef7b586da884d314bbc64a5938e363c7ca00f Signed-off-by: pr.jung --- include/storage-internal.h | 82 +++++++++++++++++++ src/storage-external-dbus.c | 78 ++++++++++++++++++ src/storage-external-dbus.h | 4 + src/storage-inhouse.c | 193 ++++++++++++++++++++++++++++++++++++++++++++ 4 files changed, 357 insertions(+) diff --git a/include/storage-internal.h b/include/storage-internal.h index 53795d2..e3bd50a 100644 --- a/include/storage-internal.h +++ b/include/storage-internal.h @@ -50,6 +50,8 @@ extern "C" { * @retval #STORAGE_ERROR_NONE Successful * @retval #STORAGE_ERROR_INVALID_PARAMETER Invalid parameter * @retval #STORAGE_ERROR_NO_DEVICE No such device + * @retval #STORAGE_ERROR_OPERATION_FAILED Operation failed + * @retval #STORAGE_ERROR_OUT_OF_MEMORY Out of memory */ int storage_get_primary_sdcard(int *storage_id, char **path); @@ -58,6 +60,86 @@ int storage_get_origin_internal_path(const char* compat, int len, char* origin); int storage_get_storage_level(const char *path, char **level); /** + * @brief This structure defines the data for receive result of mmc operations(mount/unmount/format) + */ +struct mmc_contents { + void (*mmc_cb) (int result, void* data); /**< user callback function for receive result of mmc operations */ + void* user_data; /**< input data for callback function's second-param(data) */ +}; + +/** + * @brief This API is used to mount mmc.\n + * + * @param[in] mmc_data for receive result of mount operation + * + * @return @c 0 on success, + * otherwise a negative error value + * + * @retval #STORAGE_ERROR_NONE Successful + * @retval #STORAGE_ERROR_INVALID_PARAMETER Invalid parameter + * @retval #STORAGE_ERROR_NO_DEVICE No such device + * @retval #STORAGE_ERROR_OPERATION_FAILED Operation failed + * @retval #STORAGE_ERROR_OUT_OF_MEMORY Out of memory + */ +int storage_request_mount_mmc(struct mmc_contents *mmc_data); + +/** + * @brief This API is used to unmount mmc.\n + * + * @param[in] mmc_data for receive result of unmount operation + * @param[in] option type of unmount option \n + * 0 : Normal unmount \n + * (if other process still access a sdcard, \n + * unmount will be failed.) \n + * 1 : Force unmount \n + * (if other process still access a sdcard, \n + * this process will be received SIGTERM or SIGKILL.) + * + * @return @c 0 on success, + * otherwise a negative error value + * + * @retval #STORAGE_ERROR_NONE Successful + * @retval #STORAGE_ERROR_INVALID_PARAMETER Invalid parameter + * @retval #STORAGE_ERROR_NO_DEVICE No such device + * @retval #STORAGE_ERROR_OPERATION_FAILED Operation failed + * @retval #STORAGE_ERROR_OUT_OF_MEMORY Out of memory + */ +int storage_request_unmount_mmc(struct mmc_contents *mmc_data, int option); + +/** + * @brief This API is used to format mmc.\n + * + * @param[in] mmc_data for receive result of format operation + * + * @return @c 0 on success, + * otherwise a negative error value + * + * @retval #STORAGE_ERROR_NONE Successful + * @retval #STORAGE_ERROR_INVALID_PARAMETER Invalid parameter + * @retval #STORAGE_ERROR_NO_DEVICE No such device + * @retval #STORAGE_ERROR_OPERATION_FAILED Operation failed + * @retval #STORAGE_ERROR_OUT_OF_MEMORY Out of memory + */ +int storage_request_format_mmc(struct mmc_contents *mmc_data); + +/** + * @brief This API is used to format mmc.\n + * + * @param[in] mmc_data for receive result of format operation + * @param[in] option FMT_NORMAL is 0, FMT_FORCE is 1 + * + * @return @c 0 on success, + * otherwise a negative error value + * + * @retval #STORAGE_ERROR_NONE Successful + * @retval #STORAGE_ERROR_INVALID_PARAMETER Invalid parameter + * @retval #STORAGE_ERROR_NO_DEVICE No such device + * @retval #STORAGE_ERROR_OPERATION_FAILED Operation failed + * @retval #STORAGE_ERROR_OUT_OF_MEMORY Out of memory + */ +int storage_format_mmc(struct mmc_contents *mmc_data, int option); + +/** * @} */ diff --git a/src/storage-external-dbus.c b/src/storage-external-dbus.c index f070e8f..5d45189 100755 --- a/src/storage-external-dbus.c +++ b/src/storage-external-dbus.c @@ -51,6 +51,11 @@ struct storage_ext_callback { guint block_id; }; +typedef struct { + dbus_pending_cb func; + void *data; +} pending_call_data; + static dd_list *changed_list; static void storage_ext_release_internal(storage_ext_device *dev) @@ -118,6 +123,79 @@ static GDBusConnection *get_dbus_connection(void) return conn; } +static void _cb_pending(GDBusConnection *conn, + GAsyncResult *res, + gpointer user_data) +{ + GVariant *reply = NULL; + GError *err = NULL; + pending_call_data *data = (pending_call_data *)user_data; + + reply = g_dbus_connection_call_finish(conn, res, &err); + if (!reply || err) { + if (!err) + g_set_error(&err, G_IO_ERROR, G_IO_ERROR_FAILED, + "Error during g_dbus_connection_call"); + + if (data && data->func) + data->func(NULL, data->data, err); + goto out; + } + + if (data && data->func) + data->func(reply, data->data, err); +out: + if (err) + g_error_free(err); + if (data) + free(data); +} + +int dbus_method_async_with_reply_var(const char *dest, const char *path, + const char *iface, const char *method, GVariant *param, + dbus_pending_cb cb, int timeout, void *data) +{ + GDBusConnection *conn; + pending_call_data *pdata = NULL; + int ret = 0; + + if (!dest || !path || !iface || !method) + return -EINVAL; + + if (timeout < -1) { + _E("wrong timeout %d", timeout); + return -EINVAL; + } + + conn = get_dbus_connection(); + if (!conn) { + _E("fail to get dbus connection"); //LCOV_EXCL_LINE + return -1; + } + + if (cb) { + pdata = (pending_call_data*)malloc(sizeof(pending_call_data)); + if (!pdata) { + ret = -ENOMEM; + goto err; + } + + pdata->func = cb; + pdata->data = data; + } + + g_dbus_connection_call(conn, dest, path, iface, method, + param, NULL, G_DBUS_CALL_FLAGS_NONE, timeout, NULL, + (GAsyncReadyCallback)_cb_pending, + pdata); + + return ret; +err: + if (param) + g_variant_unref(param); + return ret; +} + GVariant *dbus_method_call_sync(const gchar *dest, const gchar *path, const gchar *iface, const gchar *method, GVariant *param) { diff --git a/src/storage-external-dbus.h b/src/storage-external-dbus.h index b71d2e4..273e388 100644 --- a/src/storage-external-dbus.h +++ b/src/storage-external-dbus.h @@ -108,6 +108,10 @@ void storage_ext_unregister_device_change(storage_ext_changed_cb func); int storage_ext_get_device_info(int storage_id, storage_ext_device *info); int storage_ext_get_storage_level(const char *path, char **level); +typedef void (*dbus_pending_cb)(GVariant *var, void *user_data, GError *err); +int dbus_method_async_with_reply_var(const char *dest, const char *path, + const char *iface, const char *method, GVariant *param, + dbus_pending_cb cb, int timeout, void *data); /* storage-internal.c */ GVariant *dbus_method_call_sync(const gchar *dest, const gchar *path, const gchar *iface, const gchar *method, GVariant *param); diff --git a/src/storage-inhouse.c b/src/storage-inhouse.c index 051b6b9..d5ed6b5 100755 --- a/src/storage-inhouse.c +++ b/src/storage-inhouse.c @@ -27,6 +27,8 @@ #include "storage-internal.h" #include "storage-external-dbus.h" +#define FORMAT_TIMEOUT (120*1000) + /* Get compat path from origin Multi-user path from TZ_USER_CONTENT/.. to /opt/usr/media/.. @@ -198,3 +200,194 @@ API int storage_get_storage_level(const char *path, char **level) return STORAGE_ERROR_NONE; } + +static void mount_mmc_cb(GVariant *var, void *user_data, GError *err) +{ + struct mmc_contents *mmc_data = (struct mmc_contents*)user_data; + int mmc_ret = -1; + + _D("mount_mmc_cb called"); + + if (!var) { + _E("no message [%s]", err->message); + mmc_ret = -EBADMSG; + goto exit; + } + + g_variant_get(var, "(i)", &mmc_ret); + + _I("Mount State : %d", mmc_ret); + +exit: + if (var) + g_variant_unref(var); + (mmc_data->mmc_cb)(mmc_ret, mmc_data->user_data); +} + +API int storage_request_mount_mmc(struct mmc_contents *mmc_data) +{ + void (*mount_cb)(GVariant *, void *, GError *) = NULL; + void *data = NULL; + char *path; + int ret; + int id; + + if (mmc_data && mmc_data->mmc_cb) { + _I("Mount callback exists"); + mount_cb = mount_mmc_cb; + data = mmc_data; + } + + ret = storage_get_primary_sdcard(&id, &path); + if (ret != STORAGE_ERROR_NONE) + return ret; + + ret = dbus_method_async_with_reply_var(STORAGE_EXT_BUS_NAME, + STORAGE_EXT_PATH_MANAGER, + STORAGE_EXT_IFACE_MANAGER, + "Mount", + g_variant_new("(is)", id, ""), + mount_cb, + -1, + data); + + _I("Mount Request %s", ret == 0 ? "Success" : "Failed"); + + if (ret == -ENOMEM) + return STORAGE_ERROR_OUT_OF_MEMORY; //LCOV_EXCL_LINE System Error + if (ret < 0) + return STORAGE_ERROR_OPERATION_FAILED; //LCOV_EXCL_LINE + + return STORAGE_ERROR_NONE; +} + +static void unmount_mmc_cb(GVariant *var, void *user_data, GError *err) +{ + struct mmc_contents *mmc_data = (struct mmc_contents*)user_data; + int mmc_ret; + + _D("unmount_mmc_cb called"); + + if (!var) { + _E("no message [%s]", err->message); + mmc_ret = -EBADMSG; + goto exit; + } + + g_variant_get(var, "(i)", &mmc_ret); + + _I("Unmount State : %d", mmc_ret); + +exit: + if (var) + g_variant_unref(var); + (mmc_data->mmc_cb)(mmc_ret, mmc_data->user_data); +} + +API int storage_request_unmount_mmc(struct mmc_contents *mmc_data, int option) +{ + void (*unmount_cb)(GVariant *, void *, GError *) = NULL; + void *data = NULL; + char *path; + int ret; + int id; + + if (option < 0 || option > 1) + return STORAGE_ERROR_INVALID_PARAMETER; + + if (mmc_data && mmc_data->mmc_cb) { + _I("Unmount callback exists"); + unmount_cb = unmount_mmc_cb; + data = mmc_data; + } + + ret = storage_get_primary_sdcard(&id, &path); + if (ret != STORAGE_ERROR_NONE) + return ret; + + ret = dbus_method_async_with_reply_var(STORAGE_EXT_BUS_NAME, + STORAGE_EXT_PATH_MANAGER, + STORAGE_EXT_IFACE_MANAGER, + "Unmount", + g_variant_new("(ii)", id, option), + unmount_cb, + -1, + data); + + _I("Unmount Request %s", ret == 0 ? "Success" : "Failed"); + + if (ret == -ENOMEM) + return STORAGE_ERROR_OUT_OF_MEMORY; //LCOV_EXCL_LINE System Error + if (ret < 0) + return STORAGE_ERROR_OPERATION_FAILED; //LCOV_EXCL_LINE + + return STORAGE_ERROR_NONE; +} + +static void format_mmc_cb(GVariant *var, void *user_data, GError *err) +{ + struct mmc_contents *mmc_data = (struct mmc_contents*)user_data; + int mmc_ret; + + _D("format_mmc_cb called"); + + if (!var) { + _E("no message [%s]", err->message); + mmc_ret = -EBADMSG; + goto exit; + } + + g_variant_get(var, "(i)", &mmc_ret); + + _I("Format State : %d", mmc_ret); + +exit: + if (var) + g_variant_unref(var); + (mmc_data->mmc_cb)(mmc_ret, mmc_data->user_data); +} + +API int storage_request_format_mmc(struct mmc_contents *mmc_data) +{ + return storage_format_mmc(mmc_data, 1); +} + +API int storage_format_mmc(struct mmc_contents *mmc_data, int option) +{ + void (*format_cb)(GVariant *, void *, GError *) = NULL; + void *data = NULL; + char *path; + int ret; + int id; + + if (option < 0 || option > 1) + return STORAGE_ERROR_INVALID_PARAMETER; + + if (mmc_data && mmc_data->mmc_cb) { + _I("Format callback exists"); + format_cb = format_mmc_cb; + data = mmc_data; + } + + ret = storage_get_primary_sdcard(&id, &path); + if (ret != STORAGE_ERROR_NONE) + return ret; + + ret = dbus_method_async_with_reply_var(STORAGE_EXT_BUS_NAME, + STORAGE_EXT_PATH_MANAGER, + STORAGE_EXT_IFACE_MANAGER, + "Format", + g_variant_new("(ii)", id, option), + format_cb, + FORMAT_TIMEOUT, + data); + + _I("Format Request %s", ret == 0 ? "Success" : "Failed"); + + if (ret == -ENOMEM) + return STORAGE_ERROR_OUT_OF_MEMORY; //LCOV_EXCL_LINE System Error + if (ret < 0) + return STORAGE_ERROR_OPERATION_FAILED; //LCOV_EXCL_LINE + + return STORAGE_ERROR_NONE; +} -- 2.7.4 From 924ade1b560e5798eb261dc1187df77eaa8f3410 Mon Sep 17 00:00:00 2001 From: "pr.jung" Date: Thu, 12 Jul 2018 13:29:48 +0900 Subject: [PATCH 09/16] Check block module enabled before sending GetMmcPrimary dbus method Change-Id: I2634ba2130c35f7c3ccca9e8164ac9c73a9d47a3 Signed-off-by: pr.jung --- include/common.h | 1 + src/storage-common.c | 20 +++++++++++++++++++- src/storage-inhouse.c | 3 +++ src/storage.c | 16 ---------------- 4 files changed, 23 insertions(+), 17 deletions(-) diff --git a/include/common.h b/include/common.h index f208338..dd6f966 100644 --- a/include/common.h +++ b/include/common.h @@ -84,6 +84,7 @@ int storage_get_external_memory_size64_with_path(char *path, struct statvfs *buf #define COMPAT_DIR "/opt/usr/media" int is_compat_bind_mount(void); +int storage_ext_is_supported(void); #ifdef __cplusplus } diff --git a/src/storage-common.c b/src/storage-common.c index ba76cc8..ea8e174 100644 --- a/src/storage-common.c +++ b/src/storage-common.c @@ -16,10 +16,12 @@ * limitations under the License. */ - +#include #include #include "common.h" +#define BLOCK_CONF_FILE "/etc/storaged/block.conf" + int is_compat_bind_mount(void) { struct libmnt_table *t = NULL; @@ -50,3 +52,19 @@ int is_compat_bind_mount(void) return 0; //LCOV_EXCL_STOP } + +int storage_ext_is_supported(void) +{ + static int support = -1; + + if (support >= 0) + return support; + + if (access(BLOCK_CONF_FILE, R_OK) == 0) + support = 1; + else + support = 0; + + return support; +} + diff --git a/src/storage-inhouse.c b/src/storage-inhouse.c index d5ed6b5..2023a56 100755 --- a/src/storage-inhouse.c +++ b/src/storage-inhouse.c @@ -149,6 +149,9 @@ API int storage_get_primary_sdcard(int *storage_id, char **path) if (!storage_id || !path) return STORAGE_ERROR_INVALID_PARAMETER; + if (!storage_ext_is_supported()) + return STORAGE_ERROR_NO_DEVICE; + result = dbus_method_call_sync(STORAGE_EXT_BUS_NAME, STORAGE_EXT_PATH_MANAGER, STORAGE_EXT_IFACE_MANAGER, diff --git a/src/storage.c b/src/storage.c index 9a45507..6dfef6d 100644 --- a/src/storage.c +++ b/src/storage.c @@ -27,7 +27,6 @@ #include "log.h" #include "storage-external.h" -#define BLOCK_CONF_FILE "/etc/storaged/block.conf" #define USER_UID_START 5000 const char *dir_path[STORAGE_DIRECTORY_MAX]; @@ -61,21 +60,6 @@ void remove_device(const struct storage_ops *st) DD_LIST_REMOVE(st_int_head, st); } -int storage_ext_is_supported(void) -{ - static int support = -1; - - if (support >= 0) - return support; - - if (access(BLOCK_CONF_FILE, R_OK) == 0) - support = 1; - else - support = 0; - - return support; -} - API int storage_foreach_device_supported(storage_device_supported_cb callback, void *user_data) { const struct storage_ops *st; -- 2.7.4 From d719c4229080290e5000e3d51b7f69cd8c54543f Mon Sep 17 00:00:00 2001 From: Hyotaek Shim Date: Thu, 19 Jul 2018 11:16:24 +0900 Subject: [PATCH 10/16] Change internal storage media path returned from storage_get_directory() (Before) /opt/usr/media/XXX (After) /opt/usr/home/USER/media/XXX /opt/usr/media bind-mounted by /opt/usr/home/USER/media is workaround for supporting legacy Tizen applications based on /opt/usr/media hard coding. Strictly, public apis do not need to use legacy path (/opt/usr/media). Change-Id: Ida39777ac40587719beb8d28b8f04172c98900e5 Signed-off-by: Hyotaek Shim --- src/storage-internal.c | 8 +++----- src/storage.c | 14 +++++++------- 2 files changed, 10 insertions(+), 12 deletions(-) diff --git a/src/storage-internal.c b/src/storage-internal.c index 7f5d359..0dadec9 100755 --- a/src/storage-internal.c +++ b/src/storage-internal.c @@ -15,6 +15,8 @@ */ +#include +#include #include #include #include @@ -61,11 +63,7 @@ static int internal_get_space(unsigned long long *total, unsigned long long *ava static const char *internal_get_root(void) { const char *ret; - - if (is_compat_bind_mount()) - ret = COMPAT_DIR; - else - ret = tzplatform_getenv(TZ_USER_CONTENT); + ret = tzplatform_uid_getenv(getuid(), TZ_USER_CONTENT); return ret; } diff --git a/src/storage.c b/src/storage.c index 6dfef6d..b0d6465 100644 --- a/src/storage.c +++ b/src/storage.c @@ -72,7 +72,7 @@ API int storage_foreach_device_supported(storage_device_supported_cb callback, v return STORAGE_ERROR_INVALID_PARAMETER; } - if (getuid() < USER_UID_START) + if (getuid() <= USER_UID_START) user = false; DD_LIST_FOREACH(st_int_head, elem, st) { @@ -116,7 +116,7 @@ API int storage_get_root_directory(int storage_id, char **path) return STORAGE_ERROR_INVALID_PARAMETER; } - if (getuid() < USER_UID_START) + if (getuid() <= USER_UID_START) user = false; /* internal storage */ @@ -124,8 +124,8 @@ API int storage_get_root_directory(int storage_id, char **path) if (st->storage_id != storage_id) continue; if (!user) { - _E("Only Tizen applications and user session daemons can use \ - storage_get_root_directory(id , ...)"); + _E("Only Tizen applications and user session daemons can use\ + storage_get_root_directory(INTERNAL_STORAGE_ID, ...)"); return STORAGE_ERROR_INVALID_PARAMETER; } @@ -189,13 +189,13 @@ API int storage_get_directory(int storage_id, storage_directory_e type, char **p break; } - if (getuid() < USER_UID_START) + if (getuid() <= USER_UID_START) user = false; if (found && st) { if (!user) { - _E("Only Tizen applications and user session daemons can use \ - storage_get_directory(id, ...)"); + _E("Only Tizen applications and user session daemons can use\ + storage_get_directory(INTERNAL_STORAGE_ID, ...)"); return STORAGE_ERROR_INVALID_PARAMETER; } -- 2.7.4 From 5c437ddef63be7a6aa22966f7da70b3380441294 Mon Sep 17 00:00:00 2001 From: Hyotaek Shim Date: Fri, 20 Jul 2018 10:57:16 +0900 Subject: [PATCH 11/16] Minor fixes: comments, license .. Change-Id: I09d102eae0b5990057c0492f52d5155243deff24 Signed-off-by: Hyotaek Shim --- LICENSE => LICENSE.Apache-2.0 | 0 packaging/libstorage.spec | 5 ++--- src/storage.c | 9 +++++---- 3 files changed, 7 insertions(+), 7 deletions(-) rename LICENSE => LICENSE.Apache-2.0 (100%) diff --git a/LICENSE b/LICENSE.Apache-2.0 similarity index 100% rename from LICENSE rename to LICENSE.Apache-2.0 diff --git a/packaging/libstorage.spec b/packaging/libstorage.spec index 57611e7..f050da4 100644 --- a/packaging/libstorage.spec +++ b/packaging/libstorage.spec @@ -1,6 +1,6 @@ Name: libstorage Summary: Library to get storage information -Version: 0.1.1 +Version: 1.0.0 Release: 0 Group: System/Libraries License: Apache-2.0 @@ -26,7 +26,6 @@ Requires: %{name} = %{version}-%{release} %description devel Library to get storage information (devel) - %prep %setup -q cp %{SOURCE1} . @@ -45,7 +44,7 @@ make %{?jobs:-j%jobs} %files %{_libdir}/*.so.* %{_sysconfdir}/storage/libstorage.conf -%license LICENSE +%license LICENSE.Apache-2.0 %manifest %{name}.manifest %files devel diff --git a/src/storage.c b/src/storage.c index b0d6465..5272b57 100644 --- a/src/storage.c +++ b/src/storage.c @@ -124,8 +124,8 @@ API int storage_get_root_directory(int storage_id, char **path) if (st->storage_id != storage_id) continue; if (!user) { - _E("Only Tizen applications and user session daemons can use\ - storage_get_root_directory(INTERNAL_STORAGE_ID, ...)"); + _E("Only apps and user session daemons are allowed " + "to use storage_get_root_directory(INTERNAL_STORAGE_ID, ...)"); return STORAGE_ERROR_INVALID_PARAMETER; } @@ -194,8 +194,9 @@ API int storage_get_directory(int storage_id, storage_directory_e type, char **p if (found && st) { if (!user) { - _E("Only Tizen applications and user session daemons can use\ - storage_get_directory(INTERNAL_STORAGE_ID, ...)"); + _E("Only apps and user session daemons are allowed " + "to use storage_get_root_directory(INTERNAL_STORAGE_ID, ...)"); + return STORAGE_ERROR_INVALID_PARAMETER; } -- 2.7.4 From 22ecb430332ba5660624ecc9cfc36ac7f84e8fcc Mon Sep 17 00:00:00 2001 From: "pr.jung" Date: Mon, 23 Jul 2018 15:52:22 +0900 Subject: [PATCH 12/16] Change tzplatform_getenv to tzplatform_uid_getenv - Cover multi-user case Change-Id: I82cb6a0621ca1e87ae4009d7797cdd963ebf57a6 Signed-off-by: pr.jung --- include/common.h | 2 ++ src/storage-inhouse.c | 22 ++++++++++++++++++++-- src/storage.c | 2 -- 3 files changed, 22 insertions(+), 4 deletions(-) diff --git a/include/common.h b/include/common.h index dd6f966..0e8f4a7 100644 --- a/include/common.h +++ b/include/common.h @@ -86,6 +86,8 @@ int storage_get_external_memory_size64_with_path(char *path, struct statvfs *buf int is_compat_bind_mount(void); int storage_ext_is_supported(void); +#define USER_UID_START 5000 + #ifdef __cplusplus } #endif diff --git a/src/storage-inhouse.c b/src/storage-inhouse.c index 2023a56..3c36f58 100755 --- a/src/storage-inhouse.c +++ b/src/storage-inhouse.c @@ -15,6 +15,8 @@ */ +#include +#include #include #include #include @@ -60,6 +62,14 @@ API int storage_get_compat_internal_path(const char* origin, int len, char* comp return -1; } + if (getuid() <= USER_UID_START) { + //LCOV_EXCL_START System Error + _E("Only apps and user session daemons are allowed " + "to use storage_get_compat_internal_path()"); + return -1; + //LCOV_EXCL_STOP + } + // this API works on place where compat path is bind-mounted if (!is_compat_bind_mount()) { //LCOV_EXCL_START System Error @@ -68,7 +78,7 @@ API int storage_get_compat_internal_path(const char* origin, int len, char* comp //LCOV_EXCL_STOP } - str = tzplatform_getenv(TZ_USER_CONTENT); + str = tzplatform_uid_getenv(getuid(), TZ_USER_CONTENT); str_len = strlen(str); if (strncmp(origin, str, str_len) != 0) { _E("Failed to match TZ_USER_CONTENT"); @@ -116,6 +126,14 @@ API int storage_get_origin_internal_path(const char* compat, int len, char* orig return -1; } + if (getuid() <= USER_UID_START) { + //LCOV_EXCL_START System Error + _E("Only apps and user session daemons are allowed " + "to use storage_get_origin_internal_path()"); + return -1; + //LCOV_EXCL_STOP + } + // this API works on place where compat path is bind-mounted if (!is_compat_bind_mount()) { //LCOV_EXCL_START System Error @@ -130,7 +148,7 @@ API int storage_get_origin_internal_path(const char* compat, int len, char* orig return -1; } - r = snprintf(origin, len, "%s%s", tzplatform_getenv(TZ_USER_CONTENT), compat + compat_len); + r = snprintf(origin, len, "%s%s", tzplatform_uid_getenv(getuid(), TZ_USER_CONTENT), compat + compat_len); if (r < 0) { //LCOV_EXCL_START System Error _E("failed to create new path"); diff --git a/src/storage.c b/src/storage.c index 5272b57..4e178f5 100644 --- a/src/storage.c +++ b/src/storage.c @@ -27,8 +27,6 @@ #include "log.h" #include "storage-external.h" -#define USER_UID_START 5000 - const char *dir_path[STORAGE_DIRECTORY_MAX]; const int tz_id[STORAGE_DIRECTORY_MAX] = { -- 2.7.4 From f508857ecc798a8634ba5e97968c1c987c4a6e39 Mon Sep 17 00:00:00 2001 From: "pr.jung" Date: Thu, 26 Jul 2018 18:09:28 +0900 Subject: [PATCH 13/16] Add comments for line coverage Change-Id: I3d1b8a61d718608eec06736191961ea8fa4a8a04 Signed-off-by: pr.jung --- src/storage-inhouse.c | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/src/storage-inhouse.c b/src/storage-inhouse.c index 3c36f58..060a36d 100755 --- a/src/storage-inhouse.c +++ b/src/storage-inhouse.c @@ -222,6 +222,7 @@ API int storage_get_storage_level(const char *path, char **level) return STORAGE_ERROR_NONE; } +//LCOV_EXCL_START Not called callback static void mount_mmc_cb(GVariant *var, void *user_data, GError *err) { struct mmc_contents *mmc_data = (struct mmc_contents*)user_data; @@ -244,6 +245,7 @@ exit: g_variant_unref(var); (mmc_data->mmc_cb)(mmc_ret, mmc_data->user_data); } +//LCOV_EXCL_STOP API int storage_request_mount_mmc(struct mmc_contents *mmc_data) { @@ -282,6 +284,7 @@ API int storage_request_mount_mmc(struct mmc_contents *mmc_data) return STORAGE_ERROR_NONE; } +//LCOV_EXCL_START Not called callback static void unmount_mmc_cb(GVariant *var, void *user_data, GError *err) { struct mmc_contents *mmc_data = (struct mmc_contents*)user_data; @@ -304,6 +307,7 @@ exit: g_variant_unref(var); (mmc_data->mmc_cb)(mmc_ret, mmc_data->user_data); } +//LCOV_EXCL_STOP API int storage_request_unmount_mmc(struct mmc_contents *mmc_data, int option) { @@ -345,6 +349,7 @@ API int storage_request_unmount_mmc(struct mmc_contents *mmc_data, int option) return STORAGE_ERROR_NONE; } +//LCOV_EXCL_START Not called callback static void format_mmc_cb(GVariant *var, void *user_data, GError *err) { struct mmc_contents *mmc_data = (struct mmc_contents*)user_data; @@ -367,6 +372,7 @@ exit: g_variant_unref(var); (mmc_data->mmc_cb)(mmc_ret, mmc_data->user_data); } +//LCOV_EXCL_STOP API int storage_request_format_mmc(struct mmc_contents *mmc_data) { -- 2.7.4 From b5be274f3310695f24d748b1f26a186089f423d5 Mon Sep 17 00:00:00 2001 From: "pr.jung" Date: Tue, 31 Jul 2018 14:32:45 +0900 Subject: [PATCH 14/16] Add storage_is_mounted api - Check input param path is mounted Change-Id: Ief2f3d1703273d93dcd873c17defda7b6bd6c80e Signed-off-by: pr.jung --- include/common.h | 1 + include/storage-internal.h | 13 +++++++++++++ src/statvfs.c | 2 +- src/storage-inhouse.c | 10 ++++++++++ 4 files changed, 25 insertions(+), 1 deletion(-) diff --git a/include/common.h b/include/common.h index 0e8f4a7..af8bc3f 100644 --- a/include/common.h +++ b/include/common.h @@ -85,6 +85,7 @@ int storage_get_external_memory_size64_with_path(char *path, struct statvfs *buf #define COMPAT_DIR "/opt/usr/media" int is_compat_bind_mount(void); int storage_ext_is_supported(void); +int mount_check(char *path); #define USER_UID_START 5000 diff --git a/include/storage-internal.h b/include/storage-internal.h index e3bd50a..5548d84 100644 --- a/include/storage-internal.h +++ b/include/storage-internal.h @@ -140,6 +140,19 @@ int storage_request_format_mmc(struct mmc_contents *mmc_data); int storage_format_mmc(struct mmc_contents *mmc_data, int option); /** + * @brief This API is used to format mmc.\n + * + * @param[in] path for checking mounted + * @param[out] mounted True when path is mounted + * + * @return @c 0 on success, + * otherwise a negative error value + * + * @retval #STORAGE_ERROR_NONE Successful + */ +int storage_is_mounted(char *path, bool *mounted); + +/** * @} */ diff --git a/src/statvfs.c b/src/statvfs.c index 99ed755..2c470cc 100644 --- a/src/statvfs.c +++ b/src/statvfs.c @@ -288,7 +288,7 @@ API int storage_get_internal_memory_size64(struct statvfs *buf) return 0; } -static int mount_check(char *path) +int mount_check(char *path) { int ret = false; struct mntent *mnt; diff --git a/src/storage-inhouse.c b/src/storage-inhouse.c index 060a36d..efd62db 100755 --- a/src/storage-inhouse.c +++ b/src/storage-inhouse.c @@ -418,3 +418,13 @@ API int storage_format_mmc(struct mmc_contents *mmc_data, int option) return STORAGE_ERROR_NONE; } + +int storage_is_mounted(char *path, bool *mounted) +{ + int ret; + + ret = mount_check(path); + + *mounted = ret; + return STORAGE_ERROR_NONE; +} -- 2.7.4 From e385cee34928900da2ac0cc90bc231a55fd69cc0 Mon Sep 17 00:00:00 2001 From: "pr.jung" Date: Mon, 6 Aug 2018 15:32:13 +0900 Subject: [PATCH 15/16] Add an internal api: storage_is_mounted_opt_usr Change-Id: I341f6ac3b481b827ccb381b883dddd559f040c5b Signed-off-by: pr.jung --- CMakeLists.txt | 1 + include/common.h | 2 +- include/storage-internal.h | 16 ++++++++--- packaging/libstorage.spec | 1 + src/statvfs.c | 6 ++-- src/storage-inhouse.c | 70 ++++++++++++++++++++++++++++++++++++++++++++-- 6 files changed, 85 insertions(+), 11 deletions(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index 3169d78..83b6ea6 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -18,6 +18,7 @@ SET(dependents gio-2.0 libtzplatform-config mount + blkid ) SET(pc_dependents "capi-base-common") diff --git a/include/common.h b/include/common.h index af8bc3f..5c5a8ac 100644 --- a/include/common.h +++ b/include/common.h @@ -85,7 +85,7 @@ int storage_get_external_memory_size64_with_path(char *path, struct statvfs *buf #define COMPAT_DIR "/opt/usr/media" int is_compat_bind_mount(void); int storage_ext_is_supported(void); -int mount_check(char *path); +int mount_check(const char *path); #define USER_UID_START 5000 diff --git a/include/storage-internal.h b/include/storage-internal.h index 5548d84..9aba117 100644 --- a/include/storage-internal.h +++ b/include/storage-internal.h @@ -139,18 +139,26 @@ int storage_request_format_mmc(struct mmc_contents *mmc_data); */ int storage_format_mmc(struct mmc_contents *mmc_data, int option); +typedef enum { + STORAGE_PART_ERROR = -1, /**< Checking partition is failed */ + STORAGE_PART_NOT_MOUNTED = 0, /**< Partition is not mounted */ + STORAGE_PART_MOUNTED = 1, /**< Partition is mounted */ + STORAGE_PART_NOT_SUPPORTED = 2, /**< Partition is not supported */ +} storage_part_mount_e; + /** - * @brief This API is used to format mmc.\n + * @brief This API is used to check user data partition is mounted.\n * - * @param[in] path for checking mounted - * @param[out] mounted True when path is mounted + * @param[out] mounted 1 when user partition is mounted, 0 when user partition is not mounted, 2 when user partition is not supported(2 partitions), and Less then 0 when error return. * * @return @c 0 on success, * otherwise a negative error value * * @retval #STORAGE_ERROR_NONE Successful + * @retval #STORAGE_ERROR_OPERATION_FAILED Operation failed + * @retval #STORAGE_ERROR_OUT_OF_MEMORY Out of memory */ -int storage_is_mounted(char *path, bool *mounted); +int storage_is_mounted_opt_usr(storage_part_mount_e *mounted); /** * @} diff --git a/packaging/libstorage.spec b/packaging/libstorage.spec index f050da4..59de131 100644 --- a/packaging/libstorage.spec +++ b/packaging/libstorage.spec @@ -14,6 +14,7 @@ BuildRequires: pkgconfig(glib-2.0) BuildRequires: pkgconfig(gio-2.0) BuildRequires: pkgconfig(libtzplatform-config) BuildRequires: pkgconfig(mount) +BuildRequires: pkgconfig(blkid) %description development package of library to get storage diff --git a/src/statvfs.c b/src/statvfs.c index 2c470cc..3ec9ace 100644 --- a/src/statvfs.c +++ b/src/statvfs.c @@ -288,7 +288,7 @@ API int storage_get_internal_memory_size64(struct statvfs *buf) return 0; } -int mount_check(char *path) +int mount_check(const char *path) { int ret = false; struct mntent *mnt; @@ -341,7 +341,7 @@ int storage_get_external_memory_size_with_path(char *path, struct statvfs *buf) } } - if (!mount_check(ext_path)) + if (!mount_check((const char *)ext_path)) goto out_nodev; ret = storage_ext_get_statvfs(ext_path, &temp); @@ -385,7 +385,7 @@ int storage_get_external_memory_size64_with_path(char *path, struct statvfs *buf } } - if (!mount_check(ext_path)) + if (!mount_check((const char *)ext_path)) goto out_nodev; ret = storage_ext_get_statvfs_size64(ext_path, buf); diff --git a/src/storage-inhouse.c b/src/storage-inhouse.c index efd62db..3c06157 100755 --- a/src/storage-inhouse.c +++ b/src/storage-inhouse.c @@ -22,6 +22,7 @@ #include #include #include +#include #include "common.h" #include "list.h" @@ -30,6 +31,7 @@ #include "storage-external-dbus.h" #define FORMAT_TIMEOUT (120*1000) +#define USER_PARTITION "user" /* Get compat path from origin Multi-user path @@ -419,12 +421,74 @@ API int storage_format_mmc(struct mmc_contents *mmc_data, int option) return STORAGE_ERROR_NONE; } -int storage_is_mounted(char *path, bool *mounted) +API int storage_is_mounted_opt_usr(storage_part_mount_e *mounted) { + blkid_cache cache = NULL; + blkid_dev_iterate iter; + blkid_dev dev; int ret; + bool found = false; - ret = mount_check(path); + ret = blkid_get_cache(&cache, NULL); + if (ret < 0) { + _E("Failed to get cache"); //LCOV_EXCL_LINE + *mounted = STORAGE_PART_ERROR; //LCOV_EXCL_LINE + return STORAGE_ERROR_OPERATION_FAILED; //LCOV_EXCL_LINE + } + + ret = blkid_probe_all(cache); + if (ret < 0) { + _E("Failed to probe all block devices"); //LCOV_EXCL_LINE + *mounted = STORAGE_PART_ERROR; //LCOV_EXCL_LINE + return STORAGE_ERROR_OPERATION_FAILED; //LCOV_EXCL_LINE + } + + iter = blkid_dev_iterate_begin(cache); + if (!iter) { + _E("Failed to get iterate"); //LCOV_EXCL_LINE + *mounted = STORAGE_PART_ERROR; //LCOV_EXCL_LINE + return STORAGE_ERROR_OUT_OF_MEMORY; //LCOV_EXCL_LINE + } + + ret = blkid_dev_set_search(iter, "LABEL", USER_PARTITION); + if (blkid_dev_next(iter, &dev) == 0) { + dev = blkid_verify(cache, dev); + if (dev) { + found = true; + _D("Partition for user data is found(LABEL=user)"); + } + } + blkid_dev_iterate_end(iter); + + if (!found) { + iter = blkid_dev_iterate_begin(cache); + if (!iter) { + _E("Failed to get iterate"); //LCOV_EXCL_LINE + *mounted = STORAGE_PART_ERROR; //LCOV_EXCL_LINE + return STORAGE_ERROR_OUT_OF_MEMORY; //LCOV_EXCL_LINE + } + + ret = blkid_dev_set_search(iter, "PARTLABEL", USER_PARTITION); + if (blkid_dev_next(iter, &dev) == 0) { + dev = blkid_verify(cache, dev); + if (dev) { + found = true; + _D("Partition for user data is found(PARTLABEL=user)"); + } + } + blkid_dev_iterate_end(iter); + } + + blkid_put_cache(cache); + + if (found) { + ret = mount_check(tzplatform_getenv(TZ_SYS_USER)); + if (ret) + *mounted = STORAGE_PART_MOUNTED; + else + *mounted = STORAGE_PART_NOT_MOUNTED; + } else + *mounted = STORAGE_PART_NOT_SUPPORTED; - *mounted = ret; return STORAGE_ERROR_NONE; } -- 2.7.4 From 49e6771fa096c14cb5feab95ad68e7bedee19cb4 Mon Sep 17 00:00:00 2001 From: Hyotaek Shim Date: Fri, 10 Aug 2018 11:12:15 +0900 Subject: [PATCH 16/16] Fix some #define and add comments regarding _FILE_OFFSET_BITS=32/64 Change-Id: I7849e96b6a1e006685e2c6be953b0092dc953f86 Signed-off-by: Hyotaek Shim --- include/storage.h | 24 +++++++++--------------- src/statvfs.c | 4 ++++ src/storage-external.c | 6 +++--- src/storage-internal.c | 12 ++++++------ 4 files changed, 22 insertions(+), 24 deletions(-) diff --git a/include/storage.h b/include/storage.h index 0665cf3..1b3cbec 100644 --- a/include/storage.h +++ b/include/storage.h @@ -63,14 +63,11 @@ extern "C" { * ... * @endcode */ -#ifndef __USE_FILE_OFFSET64 -extern int storage_get_internal_memory_size(struct statvfs *buf); -#else -# define storage_get_internal_memory_size storage_get_internal_memory_size64 -#endif - #ifdef __USE_FILE_OFFSET64 +#define storage_get_internal_memory_size storage_get_internal_memory_size64 extern int storage_get_internal_memory_size64(struct statvfs *buf); +#else +extern int storage_get_internal_memory_size(struct statvfs *buf); #endif @@ -93,20 +90,17 @@ extern int storage_get_internal_memory_size64(struct statvfs *buf); * ... * @endcode */ -#ifndef __USE_FILE_OFFSET64 -extern int storage_get_external_memory_size(struct statvfs *buf); -#else -# ifdef __REDIRECT_NTH +#ifdef __USE_FILE_OFFSET64 +#ifdef __REDIRECT_NTH extern int __REDIRECT_NTH(storage_get_external_memory_size, (struct statvfs *buf), storage_get_external_memory_size64) __nonnull((1)); -# else -# define storage_get_external_memory_size storage_get_external_memory_size64 -# endif +#else +#define storage_get_external_memory_size storage_get_external_memory_size64 #endif - -#ifdef __USE_FILE_OFFSET64 extern int storage_get_external_memory_size64(struct statvfs *buf); +#else +extern int storage_get_external_memory_size(struct statvfs *buf); #endif diff --git a/src/statvfs.c b/src/statvfs.c index 3ec9ace..0085d49 100644 --- a/src/statvfs.c +++ b/src/statvfs.c @@ -223,6 +223,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; @@ -257,6 +258,7 @@ API int storage_get_internal_memory_size(struct statvfs *buf) return 0; } +/* 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; @@ -313,6 +315,7 @@ 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; @@ -358,6 +361,7 @@ out_nodev: return 0; } +/* 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; diff --git a/src/storage-external.c b/src/storage-external.c index aee5d6c..a0647a5 100755 --- a/src/storage-external.c +++ b/src/storage-external.c @@ -105,10 +105,10 @@ int storage_ext_get_space(int storage_id, } if (state >= STORAGE_STATE_MOUNTED) { -#ifndef __USE_FILE_OFFSET64 - ret = storage_get_external_memory_size_with_path(dev->mount_point, &s); -#else +#ifdef __USE_FILE_OFFSET64 ret = storage_get_external_memory_size64_with_path(dev->mount_point, &s); +#else + ret = storage_get_external_memory_size_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); //LCOV_EXCL_LINE diff --git a/src/storage-internal.c b/src/storage-internal.c index 0dadec9..2f8a796 100755 --- a/src/storage-internal.c +++ b/src/storage-internal.c @@ -29,10 +29,10 @@ #include "common.h" #include "log.h" -#ifndef __USE_FILE_OFFSET64 -int __WEAK__ storage_get_internal_memory_size(struct statvfs *buf); -#else +#ifdef __USE_FILE_OFFSET64 int __WEAK__ storage_get_internal_memory_size64(struct statvfs *buf); +#else +int __WEAK__ storage_get_internal_memory_size(struct statvfs *buf); #endif static int internal_get_state(void) @@ -45,10 +45,10 @@ static int internal_get_space(unsigned long long *total, unsigned long long *ava struct statvfs s; int ret; -#ifndef __USE_FILE_OFFSET64 - ret = storage_get_internal_memory_size(&s); -#else +#ifdef __USE_FILE_OFFSET64 ret = storage_get_internal_memory_size64(&s); +#else + ret = storage_get_internal_memory_size(&s); #endif if (ret < 0) return -EPERM; -- 2.7.4