Use private dbus connection for synchronous calls.
[platform/core/system/libstorage.git] / src / storage-inhouse.c
index 5a9ca1f..ef71f98 100755 (executable)
 #include <errno.h>
 #include <tzplatform_config.h>
 #include <blkid.h>
+#include <libsyscommon/libgdbus.h>
+#include <libsyscommon/common.h>
 
 #include "common.h"
-#include "list.h"
 #include "log.h"
 #include "storage-internal.h"
 #include "storage-external-dbus.h"
 
 #define FORMAT_TIMEOUT (120*1000)
 #define USER_PARTITION "user"
+#define CONTAINER_USER_PARTITION "contain-user"
 
 /*
        Get compat path from origin Multi-user path
@@ -163,8 +165,10 @@ API int storage_get_origin_internal_path(const char* compat, int len, char* orig
 
 API int storage_get_primary_sdcard(int *storage_id, char **path)
 {
-       GVariant *result;
-       storage_ext_device info;
+       GVariant *reply;
+       int ret, ret_dbus;
+       char *reply_mount_point = NULL;
+       int reply_id;
 
        if (!storage_id || !path)
                return STORAGE_ERROR_INVALID_PARAMETER;
@@ -172,53 +176,75 @@ API int storage_get_primary_sdcard(int *storage_id, char **path)
        if (!storage_ext_is_supported())
                return STORAGE_ERROR_NOT_SUPPORTED;
 
-       result = dbus_method_call_sync(STORAGE_EXT_BUS_NAME,
+       dbus_handle_h dbus_handle = gdbus_get_connection(G_BUS_TYPE_SYSTEM, true);
+       if (dbus_handle == NULL) {
+               _E("Failed to get dbus connection");
+               return STORAGE_ERROR_OPERATION_FAILED;
+       }
+
+       ret_dbus = gdbus_priv_call_sync_with_reply(dbus_handle,
+                       STORAGE_EXT_BUS_NAME,
                        STORAGE_EXT_PATH_MANAGER,
                        STORAGE_EXT_IFACE_MANAGER,
                        "GetMmcPrimary",
-                       NULL);
-       if (!result) {
+                       NULL,
+                       &reply);
+
+       gdbus_free_connection(dbus_handle);
+
+       if (ret_dbus < 0) {
                //LCOV_EXCL_START System Error
                _E("Failed to get primary sdcard partition"); //LCOV_EXCL_LINE
                return STORAGE_ERROR_OPERATION_FAILED; //LCOV_EXCL_LINE
                //LCOV_EXCL_STOP
        }
 
-       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);
+       if (!g_variant_get_safe(reply, "(issssssisibii)",
+                       NULL, NULL, NULL,
+                       NULL, NULL,
+                       NULL, NULL,
+                       NULL, &reply_mount_point,
+                       NULL, NULL,
+                       NULL, &reply_id)) {
+               g_variant_unref(reply);
+               return STORAGE_ERROR_OPERATION_FAILED;
+       }
 
-       g_variant_unref(result);
+       g_variant_unref(reply);
 
-       if (info.storage_id < 0)
-               return STORAGE_ERROR_NO_DEVICE;
+       if (reply_id < 0) {
+               ret = STORAGE_ERROR_NO_DEVICE;
+               goto out;
+       }
 
-       *path = strdup(info.mount_point);
-       if (*path == NULL)
-               return STORAGE_ERROR_OUT_OF_MEMORY; //LCOV_EXCL_LINE System Error
+       *path = strdup(reply_mount_point);
+       if (*path == NULL) {
+               ret = STORAGE_ERROR_OUT_OF_MEMORY; //LCOV_EXCL_LINE System Error
+               goto out;
+       }
 
-       *storage_id = info.storage_id;
+       *storage_id = reply_id;
 
-       return STORAGE_ERROR_NONE;
+       ret = STORAGE_ERROR_NONE;
+out:
+       g_free(reply_mount_point);
+
+       return ret;
 }
 
 API int storage_get_storage_level(const char *path, char **level)
 {
-       int ret;
+       int ret_level;
 
        if (!level || !path)
                return STORAGE_ERROR_INVALID_PARAMETER;
 
-       ret = storage_ext_get_storage_level(path, level);
-       if (ret == -ENOMEM)
+       ret_level = storage_ext_get_storage_level(path, level);
+       if (ret_level == -ENOMEM)
                return STORAGE_ERROR_OUT_OF_MEMORY; //LCOV_EXCL_LINE System Error
-       else if (ret == -EINVAL)
+       else if (ret_level == -EINVAL)
                return STORAGE_ERROR_INVALID_PARAMETER;
-       else if (ret < 0)
+       else if (ret_level < 0)
                return STORAGE_ERROR_OPERATION_FAILED;
 
        return STORAGE_ERROR_NONE;
@@ -254,7 +280,7 @@ 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 ret_val;
        int id;
 
        if (mmc_data && mmc_data->mmc_cb) {
@@ -263,11 +289,15 @@ API int storage_request_mount_mmc(struct mmc_contents *mmc_data)
                data = mmc_data;
        }
 
-       ret = storage_get_primary_sdcard(&id, &path);
-       if (ret != STORAGE_ERROR_NONE)
-               return ret;
+       ret_val = storage_get_primary_sdcard(&id, &path);
+       if (ret_val != STORAGE_ERROR_NONE)
+               return ret_val;
+//LCOV_EXCL_START System Error
+       if (path)
+               free(path);
+//LCOV_EXCL_STOP
 
-       ret = dbus_method_async_with_reply_var(STORAGE_EXT_BUS_NAME,
+       ret_val = gdbus_call_async_with_reply(STORAGE_EXT_BUS_NAME,
                        STORAGE_EXT_PATH_MANAGER,
                        STORAGE_EXT_IFACE_MANAGER,
                        "Mount",
@@ -276,11 +306,11 @@ API int storage_request_mount_mmc(struct mmc_contents *mmc_data)
                        -1,
                        data);
 
-       _I("Mount Request %s", ret == 0 ? "Success" : "Failed");
+       _I("Mount Request %s", ret_val == 0 ? "Success" : "Failed");
 
-       if (ret == -ENOMEM)
+       if (ret_val == -ENOMEM)
                return STORAGE_ERROR_OUT_OF_MEMORY; //LCOV_EXCL_LINE System Error
-       if (ret < 0)
+       if (ret_val < 0)
                return STORAGE_ERROR_OPERATION_FAILED; //LCOV_EXCL_LINE
 
        return STORAGE_ERROR_NONE;
@@ -316,7 +346,7 @@ 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 ret_val;
        int id;
 
        if (option < 0 || option > 1)
@@ -328,11 +358,15 @@ API int storage_request_unmount_mmc(struct mmc_contents *mmc_data, int option)
                data = mmc_data;
        }
 
-       ret = storage_get_primary_sdcard(&id, &path);
-       if (ret != STORAGE_ERROR_NONE)
-               return ret;
+       ret_val = storage_get_primary_sdcard(&id, &path);
+       if (ret_val != STORAGE_ERROR_NONE)
+               return ret_val;
+//LCOV_EXCL_START System Error
+       if (path)
+               free(path);
+//LCOV_EXCL_STOP
 
-       ret = dbus_method_async_with_reply_var(STORAGE_EXT_BUS_NAME,
+       ret_val = gdbus_call_async_with_reply(STORAGE_EXT_BUS_NAME,
                        STORAGE_EXT_PATH_MANAGER,
                        STORAGE_EXT_IFACE_MANAGER,
                        "Unmount",
@@ -341,11 +375,11 @@ API int storage_request_unmount_mmc(struct mmc_contents *mmc_data, int option)
                        -1,
                        data);
 
-       _I("Unmount Request %s", ret == 0 ? "Success" : "Failed");
+       _I("Unmount Request %s", ret_val == 0 ? "Success" : "Failed");
 
-       if (ret == -ENOMEM)
+       if (ret_val == -ENOMEM)
                return STORAGE_ERROR_OUT_OF_MEMORY; //LCOV_EXCL_LINE System Error
-       if (ret < 0)
+       if (ret_val < 0)
                return STORAGE_ERROR_OPERATION_FAILED; //LCOV_EXCL_LINE
 
        return STORAGE_ERROR_NONE;
@@ -386,7 +420,7 @@ 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 ret_val;
        int id;
 
        if (option < 0 || option > 1)
@@ -398,11 +432,15 @@ API int storage_format_mmc(struct mmc_contents *mmc_data, int option)
                data = mmc_data;
        }
 
-       ret = storage_get_primary_sdcard(&id, &path);
-       if (ret != STORAGE_ERROR_NONE)
-               return ret;
+       ret_val = storage_get_primary_sdcard(&id, &path);
+       if (ret_val != STORAGE_ERROR_NONE)
+               return ret_val;
+//LCOV_EXCL_START System Error
+       if (path)
+               free(path);
+//LCOV_EXCL_STOP
 
-       ret = dbus_method_async_with_reply_var(STORAGE_EXT_BUS_NAME,
+       ret_val = gdbus_call_async_with_reply(STORAGE_EXT_BUS_NAME,
                        STORAGE_EXT_PATH_MANAGER,
                        STORAGE_EXT_IFACE_MANAGER,
                        "Format",
@@ -411,11 +449,11 @@ API int storage_format_mmc(struct mmc_contents *mmc_data, int option)
                        FORMAT_TIMEOUT,
                        data);
 
-       _I("Format Request %s", ret == 0 ? "Success" : "Failed");
+       _I("Format Request %s", ret_val == 0 ? "Success" : "Failed");
 
-       if (ret == -ENOMEM)
+       if (ret_val == -ENOMEM)
                return STORAGE_ERROR_OUT_OF_MEMORY; //LCOV_EXCL_LINE System Error
-       if (ret < 0)
+       if (ret_val < 0)
                return STORAGE_ERROR_OPERATION_FAILED; //LCOV_EXCL_LINE
 
        return STORAGE_ERROR_NONE;
@@ -426,21 +464,23 @@ 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;
+       int ret_val;
        bool found = false;
+       char *user_label = libsys_is_container()? CONTAINER_USER_PARTITION: USER_PARTITION;
 
        if (!mounted)
                return STORAGE_ERROR_INVALID_PARAMETER;
 
-       ret = blkid_get_cache(&cache, NULL);
-       if (ret < 0) {
+       _D("Find user partition label: %s", user_label);
+       ret_val = blkid_get_cache(&cache, NULL);
+       if (ret_val < 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) {
+       ret_val = blkid_probe_all(cache);
+       if (ret_val < 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
@@ -453,12 +493,12 @@ API int storage_is_mounted_opt_usr(storage_part_mount_e *mounted)
                return STORAGE_ERROR_OUT_OF_MEMORY; //LCOV_EXCL_LINE
        }
 
-       ret = blkid_dev_set_search(iter, "LABEL", USER_PARTITION);
+       ret_val = blkid_dev_set_search(iter, "LABEL", user_label);
        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)");
+                       _D("Partition for user data is found(LABEL=%s)", user_label);
                }
        }
        blkid_dev_iterate_end(iter);
@@ -471,12 +511,12 @@ API int storage_is_mounted_opt_usr(storage_part_mount_e *mounted)
                        return STORAGE_ERROR_OUT_OF_MEMORY; //LCOV_EXCL_LINE
                }
 
-               ret = blkid_dev_set_search(iter, "PARTLABEL", USER_PARTITION);
+               ret_val = blkid_dev_set_search(iter, "PARTLABEL", user_label);
                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)");
+                               _D("Partition for user data is found(PARTLABEL=%s)", user_label);
                        }
                }
                blkid_dev_iterate_end(iter);
@@ -485,8 +525,8 @@ API int storage_is_mounted_opt_usr(storage_part_mount_e *mounted)
        blkid_put_cache(cache);
 
        if (found) {
-               ret = mount_check(tzplatform_getenv(TZ_SYS_USER));
-               if (ret)
+               ret_val = mount_check(tzplatform_getenv(TZ_SYS_USER));
+               if (ret_val)
                        *mounted = STORAGE_PART_MOUNTED;
                else
                        *mounted = STORAGE_PART_NOT_MOUNTED;