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>
30 #include <tzplatform_config.h>
31 #include <libsyscommon/libgdbus.h>
32 #include <libsyscommon/list.h>
35 #include "storage-external-dbus.h"
37 #define CHECK_STR(a) (a ? a : "")
39 #define STORAGE_EXT_GET_LIST "GetDeviceList"
40 #define STORAGE_EXT_GET_STATVFS "GetStatvfs"
41 #define STORAGE_EXT_GET_STORAGE_LEVEL "GetStorageLevel"
43 #define STORAGE_EXT_DEVICE_CHANGED "DeviceChanged"
44 #define STORAGE_EXT_DEVICE_ADDED "DeviceAdded"
45 #define STORAGE_EXT_DEVICE_REMOVED "DeviceRemoved"
46 #define STORAGE_EXT_DEVICE_BLOCKED "DeviceBlocked"
48 #define DBUS_REPLY_TIMEOUT (-1)
50 #define GET_DBUS_CONN_OR_EXIT() \
52 dbus_handle_h dbus_handle = gdbus_get_connection(G_BUS_TYPE_SYSTEM, true); \
53 if (dbus_handle == NULL) { \
54 _E("Failed to get dbus connection"); \
61 struct storage_ext_callback {
62 storage_ext_changed_cb func;
67 static GList *changed_list;
69 static void storage_ext_release_internal(storage_ext_device *dev)
77 free(dev->fs_version);
79 free(dev->mount_point);
82 void storage_ext_release_device(storage_ext_device **dev)
86 storage_ext_release_internal(*dev);
91 void storage_ext_release_list(GList **list)
93 storage_ext_device *dev;
99 SYS_G_LIST_FOREACH(*list, elem, dev) {
100 storage_ext_release_internal(dev);
108 int storage_ext_get_list(GList **list)
112 storage_ext_device *elem, info;
118 dbus_handle_h dbus_handle = GET_DBUS_CONN_OR_EXIT();
120 ret_dbus = gdbus_priv_call_sync_with_reply(dbus_handle,
121 STORAGE_EXT_BUS_NAME,
122 STORAGE_EXT_PATH_MANAGER,
123 STORAGE_EXT_IFACE_MANAGER,
124 STORAGE_EXT_GET_LIST,
125 g_variant_new("(s)", "all"),
128 gdbus_free_connection(dbus_handle);
131 _E("Failed to get storage_ext device info"); //LCOV_EXCL_LINE
135 if (!g_variant_get_safe(reply, "(a(issssssisibii))", &iter)) {
136 _E("Failed to get params from gvariant.");
137 g_variant_unref(reply);
141 while (g_variant_iter_loop(iter, "(issssssisibii)",
142 &info.type, &info.devnode, &info.syspath,
143 &info.fs_usage, &info.fs_type,
144 &info.fs_version, &info.fs_uuid,
145 &info.readonly, &info.mount_point,
146 &info.state, &info.primary,
147 &info.flags, &info.storage_id)) {
149 elem = (storage_ext_device *)malloc(sizeof(storage_ext_device));
151 _E("malloc() failed"); //LCOV_EXCL_LINE
156 elem->type = info.type;
157 elem->readonly = info.readonly;
158 elem->state = info.state;
159 elem->primary = info.primary;
160 elem->devnode = strdup(CHECK_STR(info.devnode));
161 elem->syspath = strdup(CHECK_STR(info.syspath));
162 elem->fs_usage = strdup(CHECK_STR(info.fs_usage));
163 elem->fs_type = strdup(CHECK_STR(info.fs_type));
164 elem->fs_version = strdup(CHECK_STR(info.fs_version));
165 elem->fs_uuid = strdup(CHECK_STR(info.fs_uuid));
166 elem->mount_point = strdup(CHECK_STR(info.mount_point));
167 elem->flags = info.flags;
168 elem->storage_id = info.storage_id;
170 SYS_G_LIST_APPEND(*list, elem);
173 ret = SYS_G_LIST_LENGTH(*list);
177 storage_ext_release_list(list); //LCOV_EXCL_LINE System Error
178 g_variant_iter_free(iter);
179 g_variant_unref(reply);
183 int storage_ext_get_statvfs(char *path, struct statvfs_32 *buf)
187 guint64 bsize, frsize, blocks, bfree, bavail, files, ffree, favail, fsid, flag, namemax;
191 memset(buf, 0, sizeof(struct statvfs_32));
193 dbus_handle_h dbus_handle = GET_DBUS_CONN_OR_EXIT();
195 ret_dbus = gdbus_priv_call_sync_with_reply(dbus_handle,
196 STORAGE_EXT_BUS_NAME,
197 STORAGE_EXT_PATH_STORAGE,
198 STORAGE_EXT_IFACE_STORAGE,
199 STORAGE_EXT_GET_STATVFS,
200 g_variant_new("(s)", path),
203 gdbus_free_connection(dbus_handle);
206 _E("Failed to get storage_ext device info"); //LCOV_EXCL_LINE
210 if (!g_variant_get_safe(reply, "(ttttttttttt)",
211 &bsize, &frsize, &blocks,
212 &bfree, &bavail, &files,
213 &ffree, &favail, &fsid,
215 _E("Failed to get params from gvariant.");
216 g_variant_unref(reply);
219 // %llu bsize, frsize, blocks, bfree, bavail, files, ffree, favail, fsid, flag, namemax
221 buf->f_bsize = (unsigned long)bsize;
222 buf->f_frsize = (unsigned long)frsize;
223 buf->f_blocks = (unsigned long)blocks;
224 buf->f_bfree = (unsigned long)bfree;
225 buf->f_bavail = (unsigned long)bavail;
226 buf->f_files = (unsigned long)files;
227 buf->f_ffree = (unsigned long)ffree;
228 buf->f_favail = (unsigned long)favail;
229 buf->f_fsid = (unsigned long)fsid;
230 buf->f_flag = (unsigned long)flag;
231 buf->f_namemax = (unsigned long)namemax;
233 // %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
237 int storage_ext_get_statvfs_size64(char *path, struct statvfs *buf)
244 memset(buf, 0, sizeof(struct statvfs));
246 dbus_handle_h dbus_handle = GET_DBUS_CONN_OR_EXIT();
248 ret_dbus = gdbus_priv_call_sync_with_reply(dbus_handle,
249 STORAGE_EXT_BUS_NAME,
250 STORAGE_EXT_PATH_STORAGE,
251 STORAGE_EXT_IFACE_STORAGE,
252 STORAGE_EXT_GET_STATVFS,
253 g_variant_new("(s)", path),
256 gdbus_free_connection(dbus_handle);
259 _E("Failed to get storage_ext device info"); //LCOV_EXCL_LINE
263 if (!g_variant_get_safe(reply, "(ttttttttttt)",
264 &(buf->f_bsize), &(buf->f_frsize), &(buf->f_blocks),
265 &(buf->f_bfree), &(buf->f_bavail), &(buf->f_files),
266 &(buf->f_ffree), &(buf->f_favail), &(buf->f_fsid),
267 &(buf->f_flag), &(buf->f_namemax))) {
268 _E("Failed to get params from gvariant.");
269 g_variant_unref(reply);
273 // %lu buf->f_bsize, buf->f_frsize, buf->f_fsid, buf->f_flag, buf->f_namemax
274 // %llu buf->f_blocks, buf->f_bfree, buf->f_bavail, buf->f_files, buf->f_ffree, buf->f_favail
279 int storage_ext_get_storage_level(const char *path, char **level)
284 enum tzplatform_variable id;
286 if (!strcmp(path, tzplatform_getenv(TZ_SYS_USER)))
288 else if (!strcmp(path, tzplatform_getenv(TZ_SYS_TMP)))
290 else if (!strcmp(path, tzplatform_getenv(TZ_SYS_OPT)))
297 dbus_handle_h dbus_handle = GET_DBUS_CONN_OR_EXIT();
299 ret_dbus = gdbus_priv_call_sync_with_reply(
301 STORAGE_EXT_BUS_NAME,
302 STORAGE_EXT_PATH_STORAGE,
303 STORAGE_EXT_IFACE_STORAGE,
304 STORAGE_EXT_GET_STORAGE_LEVEL,
305 g_variant_new("(i)", id),
308 gdbus_free_connection(dbus_handle);
311 _E("Failed to get %d level", id);
315 if (!g_variant_get_safe(reply, "(s)", &reply_val)) {
316 _E("Failed to get params from gvariant.");
317 g_variant_unref(reply);
321 *level = strdup(reply_val);
323 g_variant_unref(reply);
331 //LCOV_EXCL_START Not called Callback
332 static void storage_ext_device_changed(GVariant *params, enum storage_ext_state state, gpointer user_data)
334 storage_ext_device *dev;
336 struct storage_ext_callback *callback;
342 dev = calloc(1, sizeof(storage_ext_device));
346 if (!g_variant_get_safe(params, "(issssssisibii)",
362 /* Callback is called when unmount is started(DeviceBlocked signal) */
363 if (state == STORAGE_EXT_CHANGED && dev->state == STORAGE_EXT_UNMOUNTED) {
364 storage_ext_release_device(&dev);
368 SYS_G_LIST_FOREACH(changed_list, elem, callback) {
371 ret_val = callback->func(dev, state, callback->data);
373 _E("Failed to call callback for devnode(%s, %d)", dev->devnode, ret_val);
376 storage_ext_release_device(&dev);
380 //LCOV_EXCL_START Not called Callback
381 static void storage_ext_changed(GDBusConnection *conn,
389 size_t iface_len, signal_len;
390 enum storage_ext_state state;
392 if (!params || !sender || !path || !iface || !signal)
395 iface_len = strlen(iface) + 1;
396 signal_len = strlen(signal) + 1;
398 if (strncmp(iface, STORAGE_EXT_IFACE_MANAGER, iface_len))
401 if (!strncmp(signal, STORAGE_EXT_DEVICE_CHANGED, signal_len))
402 state = STORAGE_EXT_CHANGED;
404 else if (!strncmp(signal, STORAGE_EXT_DEVICE_ADDED, signal_len))
405 state = STORAGE_EXT_ADDED;
407 else if (!strncmp(signal, STORAGE_EXT_DEVICE_REMOVED, signal_len))
408 state = STORAGE_EXT_REMOVED;
410 else if (!strncmp(signal, STORAGE_EXT_DEVICE_BLOCKED, signal_len))
411 state = STORAGE_EXT_BLOCKED;
416 storage_ext_device_changed(params, state, user_data);
420 int storage_ext_register_device_change(storage_ext_changed_cb func, void *data)
423 struct storage_ext_callback *callback;
429 SYS_G_LIST_FOREACH(changed_list, elem, callback) {
430 if (callback->func != func)
432 if (callback->block_id == 0)
438 callback = (struct storage_ext_callback *)malloc(sizeof(struct storage_ext_callback));
440 //LCOV_EXCL_START System Error
441 _E("malloc() failed");
446 block_id = gdbus_signal_subscribe(NULL, NULL,
447 STORAGE_EXT_IFACE_MANAGER,
453 //LCOV_EXCL_START System Error
455 _E("Failed to subscrive bus signal");
460 callback->func = func;
461 callback->data = data;
462 callback->block_id = block_id;
464 SYS_G_LIST_APPEND(changed_list, callback);
469 void storage_ext_unregister_device_change(storage_ext_changed_cb func)
471 struct storage_ext_callback *callback;
478 SYS_G_LIST_FOREACH_SAFE(changed_list, elem, elem_n, callback) {
479 if (callback->func != func)
481 if (callback->block_id > 0)
482 gdbus_signal_unsubscribe(NULL, callback->block_id);
484 SYS_G_LIST_REMOVE(changed_list, callback);
489 int storage_ext_get_device_info(int storage_id, storage_ext_device *info)
494 dbus_handle_h dbus_handle = GET_DBUS_CONN_OR_EXIT();
496 ret_dbus = gdbus_priv_call_sync_with_reply(dbus_handle,
497 STORAGE_EXT_BUS_NAME,
498 STORAGE_EXT_PATH_MANAGER,
499 STORAGE_EXT_IFACE_MANAGER,
501 g_variant_new("(i)", storage_id),
504 gdbus_free_connection(dbus_handle);
507 _E("There is no storage with the storage id (%d)", storage_id); //LCOV_EXCL_LINE
511 if (!g_variant_get_safe(reply, "(issssssisibii)",
512 &info->type, &info->devnode, &info->syspath,
513 &info->fs_usage, &info->fs_type,
514 &info->fs_version, &info->fs_uuid,
515 &info->readonly, &info->mount_point,
516 &info->state, &info->primary,
517 &info->flags, &info->storage_id)) {
518 _E("No storage with the storage id (%d)", storage_id); //LCOV_EXCL_LINE
523 if (info->storage_id < 0) {
524 _E("No storage with the storage id (%d)", storage_id); //LCOV_EXCL_LINE
530 g_variant_unref(reply);