2 * Copyright (c) 2016 Samsung Electronics Co., Ltd All Rights Reserved
4 * Licensed under the Apache License, Version 2.0 (the License);
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
8 * http://www.apache.org/licenses/LICENSE-2.0
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an AS IS BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
22 #include <sys/statvfs.h>
24 #include <tzplatform_config.h>
29 #include "storage-external-dbus.h"
31 static dd_list *cb_list[STORAGE_CALLBACK_MAX];
33 static int storage_ext_get_dev_state(storage_ext_device *dev,
34 enum storage_ext_state blk_state,
35 storage_state_e *state)
41 case STORAGE_EXT_REMOVED:
42 *state = STORAGE_STATE_REMOVED;
44 case STORAGE_EXT_CHANGED:
46 case STORAGE_EXT_UNMOUNTED:
47 *state = STORAGE_STATE_UNMOUNTABLE;
49 case STORAGE_EXT_MOUNTED:
50 if (dev->flags & MOUNT_READONLY)
51 *state = STORAGE_STATE_MOUNTED_READ_ONLY;
53 *state = STORAGE_STATE_MOUNTED;
63 int storage_ext_foreach_device_list(storage_device_supported_cb callback, void *user_data)
67 dd_list *list = NULL, *elem;
68 storage_ext_device *dev;
69 storage_state_e state;
74 ret = storage_ext_get_list(&list);
76 _E("Failed to get external storage list from deviced (%d)", errno);
80 DD_LIST_FOREACH(list, elem, dev) {
81 ret = storage_ext_get_dev_state(dev, STORAGE_EXT_CHANGED, &state);
83 _E("Failed to get storage state (devnode:%s, ret:%d)", dev->devnode, ret);
87 ret_cb = callback(dev->storage_id, STORAGE_TYPE_EXTERNAL,
88 state, dev->mount_point, user_data);
94 storage_ext_release_list(&list);
98 static int storage_ext_state_changed(storage_ext_device *dev, enum storage_ext_state blk_state, void *data)
100 enum storage_cb_type type = (enum storage_cb_type)data;
101 struct storage_cb_info *cb_info;
103 storage_state_e state;
109 if (type != STORAGE_CALLBACK_STATE)
112 ret = storage_ext_get_dev_state(dev, blk_state, &state);
114 _E("Failed to get storage state (devnode:%s, ret:%d)", dev->devnode, ret);
118 DD_LIST_FOREACH(cb_list[STORAGE_CALLBACK_STATE], elem, cb_info)
119 cb_info->state_cb(cb_info->id, state, cb_info->user_data);
124 int storage_ext_register_cb(enum storage_cb_type type, struct storage_cb_info *info)
126 struct storage_cb_info *cb_info;
130 if (type < 0 || type >= STORAGE_CALLBACK_MAX)
136 /* check if it is the first request */
137 n = DD_LIST_LENGTH(cb_list[type]);
139 ret = storage_ext_register_device_change(storage_ext_state_changed, (void *)type);
144 /* check for the same request */
145 DD_LIST_FOREACH(cb_list[type], elem, cb_info) {
146 if (cb_info->id == info->id &&
147 cb_info->state_cb == info->state_cb)
151 /* add device changed callback to list (local) */
152 cb_info = malloc(sizeof(struct storage_cb_info));
156 memcpy(cb_info, info, sizeof(struct storage_cb_info));
157 DD_LIST_APPEND(cb_list[type], cb_info);
162 int storage_ext_unregister_cb(enum storage_cb_type type, struct storage_cb_info *info)
164 struct storage_cb_info *cb_info;
168 if (type < 0 || type >= STORAGE_CALLBACK_MAX)
174 /* search for the same element with callback */
175 DD_LIST_FOREACH(cb_list[type], elem, cb_info) {
176 if (cb_info->id == info->id &&
177 cb_info->state_cb == info->state_cb)
184 /* remove device callback from list (local) */
185 DD_LIST_REMOVE(cb_list[type], cb_info);
188 /* check if this callback is last element */
189 n = DD_LIST_LENGTH(cb_list[type]);
191 storage_ext_unregister_device_change(storage_ext_state_changed);
196 int storage_ext_get_root(int storage_id, char *path, size_t len)
198 storage_ext_device *dev;
201 if (storage_id < 0 || !path)
204 dev = calloc(1, sizeof(storage_ext_device));
210 ret = storage_ext_get_device_info(storage_id, dev);
212 _E("Cannot get the storage with id (%d, ret:%d)", storage_id, ret);
216 snprintf(path, len, "%s", dev->mount_point);
220 storage_ext_release_device(&dev);