Provide internal APIs for converting compat path 87/90187/4
authorKunhoon Baik <knhoon.baik@samsung.com>
Thu, 29 Sep 2016 00:38:48 +0000 (09:38 +0900)
committerKunhoon Baik <knhoon.baik@samsung.com>
Thu, 29 Sep 2016 02:08:16 +0000 (11:08 +0900)
Change-Id: I81aad2364f5c99a4ea252374cce4d6497093f08d

CMakeLists.txt
include/common.h
include/storage-internal.h
packaging/libstorage.spec
src/storage-common.c [new file with mode: 0644]
src/storage-external-inhouse.c [deleted file]
src/storage-inhouse.c [new file with mode: 0755]
src/storage-internal.c

index a8c5f4b3d40718194e63ce55c154ee539810697b..a4feb3836c39cfd9c3d86d4956dd079e2a89a04e 100644 (file)
@@ -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})
index a819ecc6b0ef07c623301806e3694e5e7de226a3..f208338af590fcd1ee1e3bb2088fd8b07df4cb11 100644 (file)
@@ -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
index 2040fc931f6197c0f3fd63ec7fedd31535e77bb0..391a7219eeebfacaf579b0d4bb10cc4a80f9493d 100644 (file)
@@ -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);
+
 /**
  * @}
  */
index ee26b84cc144de2aed0100c9b51cab5f1b758a72..90eae50af00259febdea6dd45d73415eae8c04f9 100644 (file)
@@ -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 (file)
index 0000000..d6d1216
--- /dev/null
@@ -0,0 +1,29 @@
+#include <libmount.h>
+#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 (executable)
index 6e7f5c4..0000000
+++ /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 <stdio.h>
-#include <stdlib.h>
-#include <string.h>
-#include <errno.h>
-
-#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 (executable)
index 0000000..e589b14
--- /dev/null
@@ -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 <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+#include <errno.h>
+#include <tzplatform_config.h>
+
+#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 <storage-internal.h>
+
+       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 <storage-internal.h>
+
+       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;
+}
index 9be10607b6d195a4f3a7d5ffe5df9f468bdc024a..a177878d254a9c9a01d0bc6cc2a1e7bc41d8a69d 100755 (executable)
@@ -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;
 }