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 struct storage_ext_callback {
51 storage_ext_changed_cb func;
56 static GList *changed_list;
58 static void storage_ext_release_internal(storage_ext_device *dev)
66 free(dev->fs_version);
68 free(dev->mount_point);
71 void storage_ext_release_device(storage_ext_device **dev)
75 storage_ext_release_internal(*dev);
80 void storage_ext_release_list(GList **list)
82 storage_ext_device *dev;
88 SYS_G_LIST_FOREACH(*list, elem, dev) {
89 storage_ext_release_internal(dev);
97 int storage_ext_get_list(GList **list)
101 storage_ext_device *elem, info;
107 ret_dbus = gdbus_call_sync_with_reply(STORAGE_EXT_BUS_NAME,
108 STORAGE_EXT_PATH_MANAGER,
109 STORAGE_EXT_IFACE_MANAGER,
110 STORAGE_EXT_GET_LIST,
111 g_variant_new("(s)", "all"),
114 _E("Failed to get storage_ext device info"); //LCOV_EXCL_LINE
118 if (!g_variant_get_safe(reply, "(a(issssssisibii))", &iter)) {
119 _E("Failed to get params from gvariant.");
120 g_variant_unref(reply);
124 while (g_variant_iter_loop(iter, "(issssssisibii)",
125 &info.type, &info.devnode, &info.syspath,
126 &info.fs_usage, &info.fs_type,
127 &info.fs_version, &info.fs_uuid,
128 &info.readonly, &info.mount_point,
129 &info.state, &info.primary,
130 &info.flags, &info.storage_id)) {
132 elem = (storage_ext_device *)malloc(sizeof(storage_ext_device));
134 _E("malloc() failed"); //LCOV_EXCL_LINE
139 elem->type = info.type;
140 elem->readonly = info.readonly;
141 elem->state = info.state;
142 elem->primary = info.primary;
143 elem->devnode = strdup(CHECK_STR(info.devnode));
144 elem->syspath = strdup(CHECK_STR(info.syspath));
145 elem->fs_usage = strdup(CHECK_STR(info.fs_usage));
146 elem->fs_type = strdup(CHECK_STR(info.fs_type));
147 elem->fs_version = strdup(CHECK_STR(info.fs_version));
148 elem->fs_uuid = strdup(CHECK_STR(info.fs_uuid));
149 elem->mount_point = strdup(CHECK_STR(info.mount_point));
150 elem->flags = info.flags;
151 elem->storage_id = info.storage_id;
153 SYS_G_LIST_APPEND(*list, elem);
156 ret = SYS_G_LIST_LENGTH(*list);
160 storage_ext_release_list(list); //LCOV_EXCL_LINE System Error
161 g_variant_iter_free(iter);
162 g_variant_unref(reply);
166 int storage_ext_get_statvfs(char *path, struct statvfs_32 *buf)
170 guint64 bsize, frsize, blocks, bfree, bavail, files, ffree, favail, fsid, flag, namemax;
174 memset(buf, 0, sizeof(struct statvfs_32));
176 ret_dbus = gdbus_call_sync_with_reply(STORAGE_EXT_BUS_NAME,
177 STORAGE_EXT_PATH_STORAGE,
178 STORAGE_EXT_IFACE_STORAGE,
179 STORAGE_EXT_GET_STATVFS,
180 g_variant_new("(s)", path),
183 _E("Failed to get storage_ext device info"); //LCOV_EXCL_LINE
187 if (!g_variant_get_safe(reply, "(ttttttttttt)",
188 &bsize, &frsize, &blocks,
189 &bfree, &bavail, &files,
190 &ffree, &favail, &fsid,
192 _E("Failed to get params from gvariant.");
193 g_variant_unref(reply);
196 // %llu bsize, frsize, blocks, bfree, bavail, files, ffree, favail, fsid, flag, namemax
198 buf->f_bsize = (unsigned long)bsize;
199 buf->f_frsize = (unsigned long)frsize;
200 buf->f_blocks = (unsigned long)blocks;
201 buf->f_bfree = (unsigned long)bfree;
202 buf->f_bavail = (unsigned long)bavail;
203 buf->f_files = (unsigned long)files;
204 buf->f_ffree = (unsigned long)ffree;
205 buf->f_favail = (unsigned long)favail;
206 buf->f_fsid = (unsigned long)fsid;
207 buf->f_flag = (unsigned long)flag;
208 buf->f_namemax = (unsigned long)namemax;
210 // %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
214 int storage_ext_get_statvfs_size64(char *path, struct statvfs *buf)
221 memset(buf, 0, sizeof(struct statvfs));
223 ret_dbus = gdbus_call_sync_with_reply(STORAGE_EXT_BUS_NAME,
224 STORAGE_EXT_PATH_STORAGE,
225 STORAGE_EXT_IFACE_STORAGE,
226 STORAGE_EXT_GET_STATVFS,
227 g_variant_new("(s)", path),
230 _E("Failed to get storage_ext device info"); //LCOV_EXCL_LINE
234 if (!g_variant_get_safe(reply, "(ttttttttttt)",
235 &(buf->f_bsize), &(buf->f_frsize), &(buf->f_blocks),
236 &(buf->f_bfree), &(buf->f_bavail), &(buf->f_files),
237 &(buf->f_ffree), &(buf->f_favail), &(buf->f_fsid),
238 &(buf->f_flag), &(buf->f_namemax))) {
239 _E("Failed to get params from gvariant.");
240 g_variant_unref(reply);
244 // %lu buf->f_bsize, buf->f_frsize, buf->f_fsid, buf->f_flag, buf->f_namemax
245 // %llu buf->f_blocks, buf->f_bfree, buf->f_bavail, buf->f_files, buf->f_ffree, buf->f_favail
250 int storage_ext_get_storage_level(const char *path, char **level)
255 enum tzplatform_variable id;
257 if (!strcmp(path, tzplatform_getenv(TZ_SYS_USER)))
259 else if (!strcmp(path, tzplatform_getenv(TZ_SYS_TMP)))
261 else if (!strcmp(path, tzplatform_getenv(TZ_SYS_OPT)))
268 ret_dbus = gdbus_call_sync_with_reply(STORAGE_EXT_BUS_NAME,
269 STORAGE_EXT_PATH_STORAGE,
270 STORAGE_EXT_IFACE_STORAGE,
271 STORAGE_EXT_GET_STORAGE_LEVEL,
272 g_variant_new("(i)", id),
275 _E("Failed to get %d level", id);
279 if (!g_variant_get_safe(reply, "(s)", &reply_val)) {
280 _E("Failed to get params from gvariant.");
281 g_variant_unref(reply);
285 *level = strdup(reply_val);
287 g_variant_unref(reply);
295 //LCOV_EXCL_START Not called Callback
296 static void storage_ext_device_changed(GVariant *params, enum storage_ext_state state, gpointer user_data)
298 storage_ext_device *dev;
300 struct storage_ext_callback *callback;
306 dev = calloc(1, sizeof(storage_ext_device));
310 if (!g_variant_get_safe(params, "(issssssisibii)",
326 /* Callback is called when unmount is started(DeviceBlocked signal) */
327 if (state == STORAGE_EXT_CHANGED && dev->state == STORAGE_EXT_UNMOUNTED) {
328 storage_ext_release_device(&dev);
332 SYS_G_LIST_FOREACH(changed_list, elem, callback) {
335 ret_val = callback->func(dev, state, callback->data);
337 _E("Failed to call callback for devnode(%s, %d)", dev->devnode, ret_val);
340 storage_ext_release_device(&dev);
344 //LCOV_EXCL_START Not called Callback
345 static void storage_ext_changed(GDBusConnection *conn,
353 size_t iface_len, signal_len;
354 enum storage_ext_state state;
356 if (!params || !sender || !path || !iface || !signal)
359 iface_len = strlen(iface) + 1;
360 signal_len = strlen(signal) + 1;
362 if (strncmp(iface, STORAGE_EXT_IFACE_MANAGER, iface_len))
365 if (!strncmp(signal, STORAGE_EXT_DEVICE_CHANGED, signal_len))
366 state = STORAGE_EXT_CHANGED;
368 else if (!strncmp(signal, STORAGE_EXT_DEVICE_ADDED, signal_len))
369 state = STORAGE_EXT_ADDED;
371 else if (!strncmp(signal, STORAGE_EXT_DEVICE_REMOVED, signal_len))
372 state = STORAGE_EXT_REMOVED;
374 else if (!strncmp(signal, STORAGE_EXT_DEVICE_BLOCKED, signal_len))
375 state = STORAGE_EXT_BLOCKED;
380 storage_ext_device_changed(params, state, user_data);
384 int storage_ext_register_device_change(storage_ext_changed_cb func, void *data)
387 struct storage_ext_callback *callback;
393 SYS_G_LIST_FOREACH(changed_list, elem, callback) {
394 if (callback->func != func)
396 if (callback->block_id == 0)
402 callback = (struct storage_ext_callback *)malloc(sizeof(struct storage_ext_callback));
404 //LCOV_EXCL_START System Error
405 _E("malloc() failed");
410 block_id = gdbus_signal_subscribe(NULL, NULL,
411 STORAGE_EXT_IFACE_MANAGER,
417 //LCOV_EXCL_START System Error
419 _E("Failed to subscrive bus signal");
424 callback->func = func;
425 callback->data = data;
426 callback->block_id = block_id;
428 SYS_G_LIST_APPEND(changed_list, callback);
433 void storage_ext_unregister_device_change(storage_ext_changed_cb func)
435 struct storage_ext_callback *callback;
442 SYS_G_LIST_FOREACH_SAFE(changed_list, elem, elem_n, callback) {
443 if (callback->func != func)
445 if (callback->block_id > 0)
446 gdbus_signal_unsubscribe(NULL, callback->block_id);
448 SYS_G_LIST_REMOVE(changed_list, callback);
453 int storage_ext_get_device_info(int storage_id, storage_ext_device *info)
458 ret_dbus = gdbus_call_sync_with_reply(STORAGE_EXT_BUS_NAME,
459 STORAGE_EXT_PATH_MANAGER,
460 STORAGE_EXT_IFACE_MANAGER,
462 g_variant_new("(i)", storage_id),
465 _E("There is no storage with the storage id (%d)", storage_id); //LCOV_EXCL_LINE
469 if (!g_variant_get_safe(reply, "(issssssisibii)",
470 &info->type, &info->devnode, &info->syspath,
471 &info->fs_usage, &info->fs_type,
472 &info->fs_version, &info->fs_uuid,
473 &info->readonly, &info->mount_point,
474 &info->state, &info->primary,
475 &info->flags, &info->storage_id)) {
476 _E("No storage with the storage id (%d)", storage_id); //LCOV_EXCL_LINE
481 if (info->storage_id < 0) {
482 _E("No storage with the storage id (%d)", storage_id); //LCOV_EXCL_LINE
488 g_variant_unref(reply);