From 709a88f8c3f9ad24eafc2788ffa50a444f274795 Mon Sep 17 00:00:00 2001 From: Kunhoon Baik Date: Thu, 29 Sep 2016 09:38:48 +0900 Subject: [PATCH] Provide internal APIs for converting compat path Change-Id: I81aad2364f5c99a4ea252374cce4d6497093f08d --- CMakeLists.txt | 4 +- include/common.h | 3 + include/storage-internal.h | 3 + packaging/libstorage.spec | 2 +- src/storage-common.c | 29 ++++++++ src/storage-external-inhouse.c | 67 ------------------ src/storage-inhouse.c | 156 +++++++++++++++++++++++++++++++++++++++++ src/storage-internal.c | 26 +------ 8 files changed, 198 insertions(+), 92 deletions(-) create mode 100644 src/storage-common.c delete mode 100755 src/storage-external-inhouse.c create mode 100755 src/storage-inhouse.c diff --git a/CMakeLists.txt b/CMakeLists.txt index a8c5f4b..a4feb38 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -48,7 +48,9 @@ SET(SRCS src/storage-internal.c src/storage-external.c src/storage-external-dbus.c - src/storage-external-inhouse.c) + src/storage-inhouse.c + src/storage-common.c + ) ADD_LIBRARY(${PROJECT_NAME} SHARED ${SRCS} ${TARGET_SRCS}) TARGET_LINK_LIBRARIES(${PROJECT_NAME} ${rpkgs_LDFLAGS}) diff --git a/include/common.h b/include/common.h index a819ecc..f208338 100644 --- a/include/common.h +++ b/include/common.h @@ -82,6 +82,9 @@ 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); +#define COMPAT_DIR "/opt/usr/media" +int is_compat_bind_mount(void); + #ifdef __cplusplus } #endif diff --git a/include/storage-internal.h b/include/storage-internal.h index 2040fc9..391a721 100644 --- a/include/storage-internal.h +++ b/include/storage-internal.h @@ -53,6 +53,9 @@ extern "C" { */ 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); + /** * @} */ diff --git a/packaging/libstorage.spec b/packaging/libstorage.spec index ee26b84..90eae50 100644 --- a/packaging/libstorage.spec +++ b/packaging/libstorage.spec @@ -1,6 +1,6 @@ Name: libstorage Summary: Library to get storage information -Version: 0.1.0 +Version: 0.1.1 Release: 0 Group: System/Libraries License: Apache-2.0 diff --git a/src/storage-common.c b/src/storage-common.c new file mode 100644 index 0000000..d6d1216 --- /dev/null +++ b/src/storage-common.c @@ -0,0 +1,29 @@ +#include +#include "common.h" + +int is_compat_bind_mount(void) +{ + struct libmnt_table *t = NULL; + int r = 0; + struct libmnt_fs *fs; + + t = mnt_new_table(); + if(!t) + return 0; + + r = mnt_table_parse_mtab(t, NULL); + if (r < 0) { + mnt_free_table(t); + return 0; + } + + fs = mnt_table_find_target(t, COMPAT_DIR, MNT_ITER_BACKWARD); + if (fs) { + // TODO : mnt_fs_get_root(fs) should be matched to tzplatform_getenv(TZ_USER_CONTENT). + mnt_free_table(t); + return 1; + } + + mnt_free_table(t); + return 0; +} diff --git a/src/storage-external-inhouse.c b/src/storage-external-inhouse.c deleted file mode 100755 index 6e7f5c4..0000000 --- a/src/storage-external-inhouse.c +++ /dev/null @@ -1,67 +0,0 @@ -/* - * Copyright (c) 2016 Samsung Electronics Co., Ltd All Rights Reserved - * - * 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. - */ - - -#include -#include -#include -#include - -#include "common.h" -#include "list.h" -#include "log.h" -#include "storage-internal.h" -#include "storage-external-dbus.h" - -API int storage_get_primary_sdcard(int *storage_id, char **path) -{ - GVariant *result; - storage_ext_device info; - - if (!storage_id || !path) - return STORAGE_ERROR_INVALID_PARAMETER; - - result = dbus_method_call_sync(STORAGE_EXT_BUS_NAME, - STORAGE_EXT_PATH_MANAGER, - STORAGE_EXT_IFACE_MANAGER, - "GetMmcPrimary", - NULL); - if (!result) { - _E("Failed to get primary sdcard partition"); //LCOV_EXCL_LINE - return STORAGE_ERROR_OPERATION_FAILED; //LCOV_EXCL_LINE - } - - g_variant_get(result, "(issssssisibii)", - &info.type, &info.devnode, &info.syspath, - &info.fs_usage, &info.fs_type, - &info.fs_version, &info.fs_uuid, - &info.readonly, &info.mount_point, - &info.state, &info.primary, - &info.flags, &info.storage_id); - - g_variant_unref(result); - - if (info.storage_id < 0) - return STORAGE_ERROR_NO_DEVICE; - - *path = strdup(info.mount_point); - if (*path == NULL) - return STORAGE_ERROR_OUT_OF_MEMORY; - - *storage_id = info.storage_id; - - return STORAGE_ERROR_NONE; -} diff --git a/src/storage-inhouse.c b/src/storage-inhouse.c new file mode 100755 index 0000000..e589b14 --- /dev/null +++ b/src/storage-inhouse.c @@ -0,0 +1,156 @@ +/* + * Copyright (c) 2016 Samsung Electronics Co., Ltd All Rights Reserved + * + * 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. + */ + + +#include +#include +#include +#include +#include + +#include "common.h" +#include "list.h" +#include "log.h" +#include "storage-internal.h" +#include "storage-external-dbus.h" + +/* + Get compat path from origin Multi-user path + from TZ_USER_CONTENT/.. to /opt/usr/media/.. + Input should be normalized path like /opt/usr/home/owner/media (TODO: internal normalization) + + Why this API should be provided? + In multi-user environment, each user has own compat content direcotry.(/opt/usr/media) + However, although system daemon operates real path, + system daemon needs to provide compat path to App if the real path is converted. + + Usage: + #include + + char dest[100]; + if(storage_get_compat_internal_path(src, sizeof(dest), dest) < 0) + // cannot convert. use src path + else + // can convert. use dest path + */ +API int storage_get_compat_internal_path(const char* origin, int len, char* compat) +{ + int r=-1; + const char* str; + + // this API works on place where compat path is bind-mounted + if(!is_compat_bind_mount()){ + _E("no compat bind mount"); + return -1; + } + + str = tzplatform_getenv(TZ_USER_CONTENT); + if(strncmp(origin,str,strlen(str))!=0){ + _E("failed to match TZ_USER_CONTENT"); + return -1; + } + + r = snprintf(compat,len,"%s/%s",COMPAT_DIR,origin+strlen(str)); + if(r < 0){ + _E("failed to create new path"); + return -1; + } + + return 0; +} + +/* + Get Multi-user path from compat path + from /opt/usr/media/.. to TZ_USER_CONTENT/.. + Input should be normalized path like /opt/usr/media (TODO: internal normalization) + + Why this API should be provided? + In multi-user environment, each user has own compat content direcotry.(/opt/usr/media) + However, although some APIs send the compat path to system daemon, + system daemon should access real path. + + Usage: + #include + + char dest[100]; + if(storage_get_origin_internal_path(src, sizeof(dest), dest) < 0) + // cannot convert. use src path + else + // can convert. use dest path +*/ +API int storage_get_origin_internal_path(const char* compat, int len, char* origin) +{ + int r; + + // this API works on place where compat path is bind-mounted + if(!is_compat_bind_mount()){ + _E("no compat bind mount"); + return -1; + } + + if(strncmp(compat,COMPAT_DIR,strlen(COMPAT_DIR))!=0){ + _E("failed to match COMPAT_DIR"); + return -1; + } + + r = snprintf(origin,len,"%s/%s",tzplatform_getenv(TZ_USER_CONTENT),compat+strlen(COMPAT_DIR)); + if(r < 0){ + _E("failed to create new path"); + return -1; + } + + return 0; +} + +API int storage_get_primary_sdcard(int *storage_id, char **path) +{ + GVariant *result; + storage_ext_device info; + + if (!storage_id || !path) + return STORAGE_ERROR_INVALID_PARAMETER; + + result = dbus_method_call_sync(STORAGE_EXT_BUS_NAME, + STORAGE_EXT_PATH_MANAGER, + STORAGE_EXT_IFACE_MANAGER, + "GetMmcPrimary", + NULL); + if (!result) { + _E("Failed to get primary sdcard partition"); //LCOV_EXCL_LINE + return STORAGE_ERROR_OPERATION_FAILED; //LCOV_EXCL_LINE + } + + g_variant_get(result, "(issssssisibii)", + &info.type, &info.devnode, &info.syspath, + &info.fs_usage, &info.fs_type, + &info.fs_version, &info.fs_uuid, + &info.readonly, &info.mount_point, + &info.state, &info.primary, + &info.flags, &info.storage_id); + + g_variant_unref(result); + + if (info.storage_id < 0) + return STORAGE_ERROR_NO_DEVICE; + + *path = strdup(info.mount_point); + if (*path == NULL) + return STORAGE_ERROR_OUT_OF_MEMORY; + + *storage_id = info.storage_id; + + return STORAGE_ERROR_NONE; +} diff --git a/src/storage-internal.c b/src/storage-internal.c index 9be1060..a177878 100755 --- a/src/storage-internal.c +++ b/src/storage-internal.c @@ -27,8 +27,6 @@ #include "common.h" #include "log.h" -#define COMPAT_DIR "/opt/usr/media" - #ifndef __USE_FILE_OFFSET64 int __WEAK__ storage_get_internal_memory_size(struct statvfs *buf); #else @@ -62,30 +60,12 @@ static int internal_get_space(unsigned long long *total, unsigned long long *ava static const char *internal_get_root(void) { - struct libmnt_table *t = NULL; - int r = 0; - struct libmnt_fs *fs; const char *ret; - ret = tzplatform_getenv(TZ_USER_CONTENT); - - t = mnt_new_table(); - if(!t) - return ret; - - r = mnt_table_parse_mtab(t, NULL); - if (r < 0) { - mnt_free_table(t); - return ret; - } - - fs = mnt_table_find_target(t, COMPAT_DIR, MNT_ITER_BACKWARD); - if (fs) { - // TODO : mnt_fs_get_root(fs) should be matched to tzplatform_getenv(TZ_USER_CONTENT). + if(is_compat_bind_mount()) ret = COMPAT_DIR; - } - - mnt_free_table(t); + else + ret = tzplatform_getenv(TZ_USER_CONTENT); return ret; } -- 2.7.4