4 * Copyright (c) 2016 Samsung Electronics Co., Ltd.
6 * Licensed under the Apache License, Version 2.0 (the License);
7 * you may not use this file except in compliance with the License.
8 * You may obtain a copy of the License at
10 * http://www.apache.org/licenses/LICENSE-2.0
12 * Unless required by applicable law or agreed to in writing, software
13 * distributed under the License is distributed on an "AS IS" BASIS,
14 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15 * See the License for the specific language governing permissions and
16 * limitations under the License.
30 #include "storage-external-dbus.h"
32 #define CHECK_STR(a) (a ? a : "")
34 #define STORAGE_EXT_GET_LIST "GetDeviceList"
36 #define DBUS_REPLY_TIMEOUT (-1)
38 static void storage_ext_release_internal(storage_ext_device *dev)
46 free(dev->fs_version);
48 free(dev->mount_point);
51 void storage_ext_release_device(storage_ext_device **dev)
55 storage_ext_release_internal(*dev);
60 void storage_ext_release_list(dd_list **list)
62 storage_ext_device *dev;
68 DD_LIST_FOREACH(*list, elem, dev) {
69 storage_ext_release_internal(dev);
77 static GDBusConnection *get_dbus_connection(void)
80 static GDBusConnection *conn;
85 #if !GLIB_CHECK_VERSION(2, 35, 0)
89 conn = g_bus_get_sync(G_BUS_TYPE_SYSTEM, NULL, &err);
92 _E("fail to get dbus connection : %s", err->message);
95 _E("fail to get dbus connection");
101 static GVariant *dbus_method_call_sync(const gchar *dest, const gchar *path,
102 const gchar *iface, const gchar *method, GVariant *param)
104 GDBusConnection *conn;
108 if (!dest || !path || !iface || !method || !param)
111 conn = get_dbus_connection();
113 _E("fail to get dbus connection");
117 ret = g_dbus_connection_call_sync(conn,
118 dest, path, iface, method,
119 param, NULL, G_DBUS_CALL_FLAGS_NONE,
123 _E("dbus method sync call failed(%s)", err->message);
126 _E("g_dbus_connection_call_sync() failed");
133 int storage_ext_get_list(dd_list **list)
137 storage_ext_device *elem, info;
143 result = dbus_method_call_sync(STORAGE_EXT_BUS_NAME,
144 STORAGE_EXT_PATH_MANAGER,
145 STORAGE_EXT_IFACE_MANAGER,
146 STORAGE_EXT_GET_LIST,
147 g_variant_new("(s)", "all"));
149 _E("Failed to get storage_ext device info");
153 g_variant_get(result, "(a(issssssisibii))", &iter);
155 while (g_variant_iter_loop(iter, "(issssssisibii)",
156 &info.type, &info.devnode, &info.syspath,
157 &info.fs_usage, &info.fs_type,
158 &info.fs_version, &info.fs_uuid,
159 &info.readonly, &info.mount_point,
160 &info.state, &info.primary,
161 &info.flags, &info.storage_id)) {
163 elem = (storage_ext_device *)malloc(sizeof(storage_ext_device));
165 _E("malloc() failed");
170 elem->type = info.type;
171 elem->readonly = info.readonly;
172 elem->state = info.state;
173 elem->primary = info.primary;
174 elem->devnode = strdup(CHECK_STR(info.devnode));
175 elem->syspath = strdup(CHECK_STR(info.syspath));
176 elem->fs_usage = strdup(CHECK_STR(info.fs_usage));
177 elem->fs_type = strdup(CHECK_STR(info.fs_type));
178 elem->fs_version = strdup(CHECK_STR(info.fs_version));
179 elem->fs_uuid = strdup(CHECK_STR(info.fs_uuid));
180 elem->mount_point = strdup(CHECK_STR(info.mount_point));
181 elem->flags = info.flags;
182 elem->storage_id = info.storage_id;
184 DD_LIST_APPEND(*list, elem);
187 ret = g_list_length(*list);
191 storage_ext_release_list(list);
192 g_variant_iter_free(iter);
193 g_variant_unref(result);