Add an internal api: storage_is_mounted_opt_usr
[platform/core/system/libstorage.git] / src / storage-inhouse.c
index d5ed6b5..3c06157 100755 (executable)
  */
 
 
+#include <unistd.h>
+#include <sys/types.h>
 #include <stdio.h>
 #include <stdlib.h>
 #include <string.h>
 #include <errno.h>
 #include <tzplatform_config.h>
+#include <blkid.h>
 
 #include "common.h"
 #include "list.h"
@@ -28,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
@@ -60,6 +64,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 +80,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 +128,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 +150,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");
@@ -149,6 +169,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,
@@ -201,6 +224,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;
@@ -223,6 +247,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)
 {
@@ -261,6 +286,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;
@@ -283,6 +309,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)
 {
@@ -324,6 +351,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;
@@ -346,6 +374,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)
 {
@@ -391,3 +420,75 @@ API int storage_format_mmc(struct mmc_contents *mmc_data, int option)
 
        return STORAGE_ERROR_NONE;
 }
+
+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 = 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;
+
+       return STORAGE_ERROR_NONE;
+}