+
+int storage_ext_get_statvfs(char *path, struct statvfs_32 *buf)
+{
+ GVariant *reply;
+ int ret_dbus;
+ guint64 bsize, frsize, blocks, bfree, bavail, files, ffree, favail, fsid, flag, namemax;
+
+ assert(buf);
+
+ memset(buf, 0, sizeof(struct statvfs_32));
+
+ dbus_handle_h dbus_handle = GET_DBUS_CONN_OR_EXIT();
+
+ ret_dbus = gdbus_priv_call_sync_with_reply(dbus_handle,
+ STORAGE_EXT_BUS_NAME,
+ STORAGE_EXT_PATH_STORAGE,
+ STORAGE_EXT_IFACE_STORAGE,
+ STORAGE_EXT_GET_STATVFS,
+ g_variant_new("(s)", path),
+ &reply);
+
+ gdbus_free_connection(dbus_handle);
+
+ if (ret_dbus < 0) {
+ _E("Failed to get storage_ext device info"); //LCOV_EXCL_LINE
+ return -EIO;
+ }
+
+ if (!g_variant_get_safe(reply, "(ttttttttttt)",
+ &bsize, &frsize, &blocks,
+ &bfree, &bavail, &files,
+ &ffree, &favail, &fsid,
+ &flag, &namemax)) {
+ _E("Failed to get params from gvariant.");
+ g_variant_unref(reply);
+ return -EIO;
+ }
+// %llu bsize, frsize, blocks, bfree, bavail, files, ffree, favail, fsid, flag, namemax
+
+ buf->f_bsize = (unsigned long)bsize;
+ buf->f_frsize = (unsigned long)frsize;
+ buf->f_blocks = (unsigned long)blocks;
+ buf->f_bfree = (unsigned long)bfree;
+ buf->f_bavail = (unsigned long)bavail;
+ buf->f_files = (unsigned long)files;
+ buf->f_ffree = (unsigned long)ffree;
+ buf->f_favail = (unsigned long)favail;
+ buf->f_fsid = (unsigned long)fsid;
+ buf->f_flag = (unsigned long)flag;
+ buf->f_namemax = (unsigned long)namemax;
+
+// %lu buf->f_bsize, buf->f_frsize, buf->f_blocks, buf->f_bfree, buf->f_bavail, buf->f_files, buf->f_ffree, buf->f_favail, buf->f_fsid, buf->f_flag, buf->f_namemax
+ return 0;
+}
+
+int storage_ext_get_statvfs_size64(char *path, struct statvfs *buf)
+{
+ GVariant *reply;
+ int ret_dbus;
+
+ assert(buf);
+
+ memset(buf, 0, sizeof(struct statvfs));
+
+ dbus_handle_h dbus_handle = GET_DBUS_CONN_OR_EXIT();
+
+ ret_dbus = gdbus_priv_call_sync_with_reply(dbus_handle,
+ STORAGE_EXT_BUS_NAME,
+ STORAGE_EXT_PATH_STORAGE,
+ STORAGE_EXT_IFACE_STORAGE,
+ STORAGE_EXT_GET_STATVFS,
+ g_variant_new("(s)", path),
+ &reply);
+
+ gdbus_free_connection(dbus_handle);
+
+ if (ret_dbus < 0) {
+ _E("Failed to get storage_ext device info"); //LCOV_EXCL_LINE
+ return -EIO;
+ }
+
+ if (!g_variant_get_safe(reply, "(ttttttttttt)",
+ &(buf->f_bsize), &(buf->f_frsize), &(buf->f_blocks),
+ &(buf->f_bfree), &(buf->f_bavail), &(buf->f_files),
+ &(buf->f_ffree), &(buf->f_favail), &(buf->f_fsid),
+ &(buf->f_flag), &(buf->f_namemax))) {
+ _E("Failed to get params from gvariant.");
+ g_variant_unref(reply);
+ return -EIO;
+ }
+
+// %lu buf->f_bsize, buf->f_frsize, buf->f_fsid, buf->f_flag, buf->f_namemax
+// %llu buf->f_blocks, buf->f_bfree, buf->f_bavail, buf->f_files, buf->f_ffree, buf->f_favail
+
+ return 0;
+}
+
+int storage_ext_get_storage_level(const char *path, char **level)
+{
+ GVariant *reply;
+ int ret_dbus;
+ char *reply_val;
+ 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;
+ }
+
+ dbus_handle_h dbus_handle = GET_DBUS_CONN_OR_EXIT();
+
+ ret_dbus = gdbus_priv_call_sync_with_reply(
+ dbus_handle,
+ STORAGE_EXT_BUS_NAME,
+ STORAGE_EXT_PATH_STORAGE,
+ STORAGE_EXT_IFACE_STORAGE,
+ STORAGE_EXT_GET_STORAGE_LEVEL,
+ g_variant_new("(i)", id),
+ &reply);
+
+ gdbus_free_connection(dbus_handle);
+
+ if (ret_dbus < 0) {
+ _E("Failed to get %d level", id);
+ return -EIO;
+ }
+
+ if (!g_variant_get_safe(reply, "(s)", &reply_val)) {
+ _E("Failed to get params from gvariant.");
+ g_variant_unref(reply);
+ return -EIO;
+ }
+
+ *level = strdup(reply_val);
+ g_free(reply_val);
+ g_variant_unref(reply);
+
+ 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)
+{
+ storage_ext_device *dev;
+ GList *elem;
+ struct storage_ext_callback *callback;
+ int ret_val;
+
+ if (!params)
+ return;
+
+ dev = calloc(1, sizeof(storage_ext_device));
+ if (!dev)
+ return;
+
+ if (!g_variant_get_safe(params, "(issssssisibii)",
+ &dev->type,
+ &dev->devnode,
+ &dev->syspath,
+ &dev->fs_usage,
+ &dev->fs_type,
+ &dev->fs_version,
+ &dev->fs_uuid,
+ &dev->readonly,
+ &dev->mount_point,
+ &dev->state,
+ &dev->primary,
+ &dev->flags,
+ &dev->storage_id))
+ return;
+
+ /* Callback is called when unmount is started(DeviceBlocked signal) */
+ if (state == STORAGE_EXT_CHANGED && dev->state == STORAGE_EXT_UNMOUNTED) {
+ storage_ext_release_device(&dev);
+ return;
+ }
+
+ SYS_G_LIST_FOREACH(changed_list, elem, callback) {
+ if (!callback->func)
+ continue;
+ ret_val = callback->func(dev, state, callback->data);
+ if (ret_val < 0)
+ _E("Failed to call callback for devnode(%s, %d)", dev->devnode, ret_val);
+ }
+
+ storage_ext_release_device(&dev);
+}
+//LCOV_EXCL_STOP
+
+//LCOV_EXCL_START Not called Callback
+static void storage_ext_changed(GDBusConnection *conn,
+ const gchar *sender,
+ const gchar *path,
+ const gchar *iface,
+ const gchar *signal,
+ GVariant *params,
+ gpointer user_data)
+{
+ size_t iface_len, signal_len;
+ enum storage_ext_state state;
+
+ if (!params || !sender || !path || !iface || !signal)
+ return;
+
+ iface_len = strlen(iface) + 1;
+ signal_len = strlen(signal) + 1;
+
+ if (strncmp(iface, STORAGE_EXT_IFACE_MANAGER, iface_len))
+ return;
+
+ if (!strncmp(signal, STORAGE_EXT_DEVICE_CHANGED, signal_len))
+ state = STORAGE_EXT_CHANGED;
+
+ else if (!strncmp(signal, STORAGE_EXT_DEVICE_ADDED, signal_len))
+ state = STORAGE_EXT_ADDED;
+
+ else if (!strncmp(signal, STORAGE_EXT_DEVICE_REMOVED, signal_len))
+ state = STORAGE_EXT_REMOVED;
+
+ else if (!strncmp(signal, STORAGE_EXT_DEVICE_BLOCKED, signal_len))
+ state = STORAGE_EXT_BLOCKED;
+
+ else
+ return;
+
+ storage_ext_device_changed(params, state, user_data);
+}
+//LCOV_EXCL_STOP
+
+int storage_ext_register_device_change(storage_ext_changed_cb func, void *data)
+{
+ guint block_id = 0;
+ struct storage_ext_callback *callback;
+ GList *elem;
+
+ if (!func)
+ return -EINVAL;
+
+ SYS_G_LIST_FOREACH(changed_list, elem, callback) {
+ if (callback->func != func)
+ continue;
+ if (callback->block_id == 0)
+ continue;
+
+ return -EEXIST;
+ }
+
+ callback = (struct storage_ext_callback *)malloc(sizeof(struct storage_ext_callback));
+ if (!callback) {
+ //LCOV_EXCL_START System Error
+ _E("malloc() failed");
+ return -ENOMEM;
+ //LCOV_EXCL_STOP
+ }
+
+ block_id = gdbus_signal_subscribe(NULL, NULL,
+ STORAGE_EXT_IFACE_MANAGER,
+ NULL,
+ storage_ext_changed,
+ NULL,
+ NULL);
+ if (block_id == 0) {
+ //LCOV_EXCL_START System Error
+ free(callback);
+ _E("Failed to subscrive bus signal");
+ return -EPERM;
+ //LCOV_EXCL_STOP
+ }
+
+ callback->func = func;
+ callback->data = data;
+ callback->block_id = block_id;
+
+ SYS_G_LIST_APPEND(changed_list, callback);
+
+ return 0;
+}
+
+void storage_ext_unregister_device_change(storage_ext_changed_cb func)
+{
+ struct storage_ext_callback *callback;
+ GList *elem;
+ GList *elem_n;
+
+ if (!func)
+ return;
+
+ SYS_G_LIST_FOREACH_SAFE(changed_list, elem, elem_n, callback) {
+ if (callback->func != func)
+ continue;
+ if (callback->block_id > 0)
+ gdbus_signal_unsubscribe(NULL, callback->block_id);
+
+ SYS_G_LIST_REMOVE(changed_list, callback);
+ free(callback);
+ }
+}
+
+int storage_ext_get_device_info(int storage_id, storage_ext_device *info)
+{
+ GVariant *reply;
+ int ret_dbus;
+
+ dbus_handle_h dbus_handle = GET_DBUS_CONN_OR_EXIT();
+
+ ret_dbus = gdbus_priv_call_sync_with_reply(dbus_handle,
+ STORAGE_EXT_BUS_NAME,
+ STORAGE_EXT_PATH_MANAGER,
+ STORAGE_EXT_IFACE_MANAGER,
+ "GetDeviceInfo",
+ g_variant_new("(i)", storage_id),
+ &reply);
+
+ gdbus_free_connection(dbus_handle);
+
+ if (ret_dbus < 0) {
+ _E("There is no storage with the storage id (%d)", storage_id); //LCOV_EXCL_LINE
+ return -ENODEV;
+ }
+
+ if (!g_variant_get_safe(reply, "(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)) {
+ _E("No storage with the storage id (%d)", storage_id); //LCOV_EXCL_LINE
+ ret_dbus = -ENODEV;
+ goto out;
+ }
+
+ if (info->storage_id < 0) {
+ _E("No storage with the storage id (%d)", storage_id); //LCOV_EXCL_LINE
+ ret_dbus = -ENODEV;
+ goto out;
+ }
+
+out:
+ g_variant_unref(reply);
+ return 0;
+}