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.
29 #include <sys/statvfs.h>
32 #include "storage-external-dbus.h"
34 #define CHECK_STR(a) (a ? a : "")
36 #define STORAGE_EXT_GET_LIST "GetDeviceList"
37 #define STORAGE_EXT_GET_STATVFS "GetStatvfs"
38 #define STORAGE_EXT_GET_STORAGE_LEVEL "GetStorageLevel"
40 #define STORAGE_EXT_DEVICE_CHANGED "DeviceChanged"
41 #define STORAGE_EXT_DEVICE_ADDED "DeviceAdded"
42 #define STORAGE_EXT_DEVICE_REMOVED "DeviceRemoved"
43 #define STORAGE_EXT_DEVICE_BLOCKED "DeviceBlocked"
45 #define DBUS_REPLY_TIMEOUT (-1)
47 struct storage_ext_callback {
48 storage_ext_changed_cb func;
53 static dd_list *changed_list;
55 static void storage_ext_release_internal(storage_ext_device *dev)
63 free(dev->fs_version);
65 free(dev->mount_point);
68 void storage_ext_release_device(storage_ext_device **dev)
72 storage_ext_release_internal(*dev);
77 void storage_ext_release_list(dd_list **list)
79 storage_ext_device *dev;
85 DD_LIST_FOREACH(*list, elem, dev) {
86 storage_ext_release_internal(dev);
94 static GDBusConnection *get_dbus_connection(void)
97 static GDBusConnection *conn;
102 #if !GLIB_CHECK_VERSION(2, 35, 0)
106 conn = g_bus_get_sync(G_BUS_TYPE_SYSTEM, NULL, &err);
108 //LCOV_EXCL_START System Error
110 _E("fail to get dbus connection : %s", err->message);
113 _E("fail to get dbus connection");
120 GVariant *dbus_method_call_sync(const gchar *dest, const gchar *path,
121 const gchar *iface, const gchar *method, GVariant *param)
123 GDBusConnection *conn;
127 if (!dest || !path || !iface || !method)
130 conn = get_dbus_connection();
132 _E("fail to get dbus connection"); //LCOV_EXCL_LINE
136 ret = g_dbus_connection_call_sync(conn,
137 dest, path, iface, method,
138 param, NULL, G_DBUS_CALL_FLAGS_NONE,
141 //LCOV_EXCL_START System Error
143 _E("dbus method sync call failed(%s)", err->message);
146 _E("g_dbus_connection_call_sync() failed");
154 int storage_ext_get_list(dd_list **list)
158 storage_ext_device *elem, info;
164 result = dbus_method_call_sync(STORAGE_EXT_BUS_NAME,
165 STORAGE_EXT_PATH_MANAGER,
166 STORAGE_EXT_IFACE_MANAGER,
167 STORAGE_EXT_GET_LIST,
168 g_variant_new("(s)", "all"));
170 _E("Failed to get storage_ext device info"); //LCOV_EXCL_LINE
174 g_variant_get(result, "(a(issssssisibii))", &iter);
176 while (g_variant_iter_loop(iter, "(issssssisibii)",
177 &info.type, &info.devnode, &info.syspath,
178 &info.fs_usage, &info.fs_type,
179 &info.fs_version, &info.fs_uuid,
180 &info.readonly, &info.mount_point,
181 &info.state, &info.primary,
182 &info.flags, &info.storage_id)) {
184 elem = (storage_ext_device *)malloc(sizeof(storage_ext_device));
186 _E("malloc() failed"); //LCOV_EXCL_LINE
191 elem->type = info.type;
192 elem->readonly = info.readonly;
193 elem->state = info.state;
194 elem->primary = info.primary;
195 elem->devnode = strdup(CHECK_STR(info.devnode));
196 elem->syspath = strdup(CHECK_STR(info.syspath));
197 elem->fs_usage = strdup(CHECK_STR(info.fs_usage));
198 elem->fs_type = strdup(CHECK_STR(info.fs_type));
199 elem->fs_version = strdup(CHECK_STR(info.fs_version));
200 elem->fs_uuid = strdup(CHECK_STR(info.fs_uuid));
201 elem->mount_point = strdup(CHECK_STR(info.mount_point));
202 elem->flags = info.flags;
203 elem->storage_id = info.storage_id;
205 DD_LIST_APPEND(*list, elem);
208 ret = g_list_length(*list);
212 storage_ext_release_list(list); //LCOV_EXCL_LINE System Error
213 g_variant_iter_free(iter);
214 g_variant_unref(result);
218 int storage_ext_get_statvfs(char *path, struct statvfs_32 *buf)
221 guint64 bsize, frsize, blocks, bfree, bavail, files, ffree, favail, fsid, flag, namemax;
225 memset(buf, 0, sizeof(struct statvfs_32));
227 result = dbus_method_call_sync(STORAGE_EXT_BUS_NAME,
228 STORAGE_EXT_PATH_STORAGE,
229 STORAGE_EXT_IFACE_STORAGE,
230 STORAGE_EXT_GET_STATVFS,
231 g_variant_new("(s)", path));
233 _E("Failed to get storage_ext device info"); //LCOV_EXCL_LINE
237 g_variant_get(result, "(ttttttttttt)",
238 &bsize, &frsize, &blocks,
239 &bfree, &bavail, &files,
240 &ffree, &favail, &fsid,
242 // %llu bsize, frsize, blocks, bfree, bavail, files, ffree, favail, fsid, flag, namemax
244 buf->f_bsize = (unsigned long)bsize;
245 buf->f_frsize = (unsigned long)frsize;
246 buf->f_blocks = (unsigned long)blocks;
247 buf->f_bfree = (unsigned long)bfree;
248 buf->f_bavail = (unsigned long)bavail;
249 buf->f_files = (unsigned long)files;
250 buf->f_ffree = (unsigned long)ffree;
251 buf->f_favail = (unsigned long)favail;
252 buf->f_fsid = (unsigned long)fsid;
253 buf->f_flag = (unsigned long)flag;
254 buf->f_namemax = (unsigned long)namemax;
256 // %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
260 int storage_ext_get_statvfs_size64(char *path, struct statvfs *buf)
266 memset(buf, 0, sizeof(struct statvfs));
268 result = dbus_method_call_sync(STORAGE_EXT_BUS_NAME,
269 STORAGE_EXT_PATH_STORAGE,
270 STORAGE_EXT_IFACE_STORAGE,
271 STORAGE_EXT_GET_STATVFS,
272 g_variant_new("(s)", path));
274 _E("Failed to get storage_ext device info"); //LCOV_EXCL_LINE
278 g_variant_get(result, "(ttttttttttt)",
279 &(buf->f_bsize), &(buf->f_frsize), &(buf->f_blocks),
280 &(buf->f_bfree), &(buf->f_bavail), &(buf->f_files),
281 &(buf->f_ffree), &(buf->f_favail), &(buf->f_fsid),
282 &(buf->f_flag), &(buf->f_namemax));
284 // %lu buf->f_bsize, buf->f_frsize, buf->f_fsid, buf->f_flag, buf->f_namemax
285 // %llu buf->f_blocks, buf->f_bfree, buf->f_bavail, buf->f_files, buf->f_ffree, buf->f_favail
290 int storage_ext_get_storage_level(enum tzplatform_variable id, char **level)
295 result = dbus_method_call_sync(STORAGE_EXT_BUS_NAME,
296 STORAGE_EXT_PATH_STORAGE,
297 STORAGE_EXT_IFACE_STORAGE,
298 STORAGE_EXT_GET_STORAGE_LEVEL,
299 g_variant_new("(i)", id));
301 _E("Failed to get %d level", id);
305 g_variant_get(result, "(s)", &tmp);
306 *level = strdup(tmp);
313 //LCOV_EXCL_START Not called Callback
314 static void storage_ext_device_changed(GVariant *params, enum storage_ext_state state, gpointer user_data)
316 storage_ext_device *dev;
318 struct storage_ext_callback *callback;
324 dev = calloc(1, sizeof(storage_ext_device));
328 g_variant_get(params, "(issssssisibii)",
343 /* Callback is called when unmount is started(DeviceBlocked signal) */
344 if (state == STORAGE_EXT_CHANGED && dev->state == STORAGE_EXT_UNMOUNTED) {
345 storage_ext_release_device(&dev);
349 DD_LIST_FOREACH(changed_list, elem, callback) {
352 ret = callback->func(dev, state, callback->data);
354 _E("Failed to call callback for devnode(%s, %d)", dev->devnode, ret);
357 storage_ext_release_device(&dev);
361 //LCOV_EXCL_START Not called Callback
362 static void storage_ext_changed(GDBusConnection *conn,
370 size_t iface_len, signal_len;
371 enum storage_ext_state state;
373 if (!params || !sender || !path || !iface || !signal)
376 iface_len = strlen(iface) + 1;
377 signal_len = strlen(signal) + 1;
379 if (strncmp(iface, STORAGE_EXT_IFACE_MANAGER, iface_len))
382 if (!strncmp(signal, STORAGE_EXT_DEVICE_CHANGED, signal_len))
383 state = STORAGE_EXT_CHANGED;
385 else if (!strncmp(signal, STORAGE_EXT_DEVICE_ADDED, signal_len))
386 state = STORAGE_EXT_ADDED;
388 else if (!strncmp(signal, STORAGE_EXT_DEVICE_REMOVED, signal_len))
389 state = STORAGE_EXT_REMOVED;
391 else if (!strncmp(signal, STORAGE_EXT_DEVICE_BLOCKED, signal_len))
392 state = STORAGE_EXT_BLOCKED;
397 storage_ext_device_changed(params, state, user_data);
401 int storage_ext_register_device_change(storage_ext_changed_cb func, void *data)
403 GDBusConnection *conn;
405 struct storage_ext_callback *callback;
411 DD_LIST_FOREACH(changed_list, elem, callback) {
412 if (callback->func != func)
414 if (callback->block_id == 0)
420 callback = (struct storage_ext_callback *)malloc(sizeof(struct storage_ext_callback));
422 //LCOV_EXCL_START System Error
423 _E("malloc() failed");
428 conn = get_dbus_connection();
430 //LCOV_EXCL_START System Error
432 _E("Failed to get dbus connection");
437 block_id = g_dbus_connection_signal_subscribe(conn,
439 STORAGE_EXT_IFACE_MANAGER,
443 G_DBUS_SIGNAL_FLAGS_NONE,
448 //LCOV_EXCL_START System Error
450 _E("Failed to subscrive bus signal");
455 callback->func = func;
456 callback->data = data;
457 callback->block_id = block_id;
459 DD_LIST_APPEND(changed_list, callback);
464 void storage_ext_unregister_device_change(storage_ext_changed_cb func)
466 GDBusConnection *conn;
467 struct storage_ext_callback *callback;
474 conn = get_dbus_connection();
476 //LCOV_EXCL_START System Error
477 _E("fail to get dbus connection");
482 DD_LIST_FOREACH_SAFE(changed_list, elem, elem_n, callback) {
483 if (callback->func != func)
485 if (callback->block_id > 0)
486 g_dbus_connection_signal_unsubscribe(conn, callback->block_id);
488 DD_LIST_REMOVE(changed_list, callback);
493 int storage_ext_get_device_info(int storage_id, storage_ext_device *info)
497 result = dbus_method_call_sync(STORAGE_EXT_BUS_NAME,
498 STORAGE_EXT_PATH_MANAGER,
499 STORAGE_EXT_IFACE_MANAGER,
501 g_variant_new("(i)", storage_id));
503 _E("There is no storage with the storage id (%d)", storage_id); //LCOV_EXCL_LINE
507 if (g_variant_check_format_string(result, "(issssssisibii)", true)) {
508 g_variant_get(result, "(issssssisibii)",
509 &info->type, &info->devnode, &info->syspath,
510 &info->fs_usage, &info->fs_type,
511 &info->fs_version, &info->fs_uuid,
512 &info->readonly, &info->mount_point,
513 &info->state, &info->primary,
514 &info->flags, &info->storage_id);
516 _E("No storage with the storage id (%d)", storage_id); //LCOV_EXCL_LINE
520 if (info->storage_id < 0) {
521 _E("No storage with the storage id (%d)", storage_id); //LCOV_EXCL_LINE
525 g_variant_unref(result);