2 * Copyright (c) 2011 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 <system_settings.h>
27 #include "storage-external.h"
29 const char *dir_path[STORAGE_DIRECTORY_MAX] = {
30 [STORAGE_DIRECTORY_IMAGES] = "Images",
31 [STORAGE_DIRECTORY_SOUNDS] = "Sounds",
32 [STORAGE_DIRECTORY_VIDEOS] = "Videos",
33 [STORAGE_DIRECTORY_CAMERA] = "Camera",
34 [STORAGE_DIRECTORY_DOWNLOADS] = "Downloads",
35 [STORAGE_DIRECTORY_MUSIC] = "Music",
36 [STORAGE_DIRECTORY_DOCUMENTS] = "Documents",
37 [STORAGE_DIRECTORY_OTHERS] = "Others",
38 [STORAGE_DIRECTORY_SYSTEM_RINGTONES] = "",
41 static dd_list *st_int_head; /* Internal storage list */
43 void add_device(const struct storage_ops *st)
45 DD_LIST_APPEND(st_int_head, st);
48 void remove_device(const struct storage_ops *st)
50 DD_LIST_REMOVE(st_int_head, st);
53 API int storage_foreach_device_supported(storage_device_supported_cb callback, void *user_data)
55 const struct storage_ops *st;
60 _E("Invalid parameter");
61 return STORAGE_ERROR_INVALID_PARAMETER;
64 DD_LIST_FOREACH(st_int_head, elem, st) {
65 ret = callback(st->storage_id, st->type, st->get_state(),
66 st->root(), user_data);
67 /* if the return value is false, will be stop to iterate */
72 ret = storage_ext_foreach_device_list(callback, user_data);
74 _E("Failed to iterate external devices (%d)", ret);
75 return STORAGE_ERROR_OPERATION_FAILED;
78 return STORAGE_ERROR_NONE;
81 API int storage_get_root_directory(int storage_id, char **path)
83 const struct storage_ops *st;
88 if (!path || storage_id < 0) {
89 _E("Invalid parameger");
90 return STORAGE_ERROR_INVALID_PARAMETER;
93 /* internal storage */
94 DD_LIST_FOREACH(st_int_head, elem, st) {
95 if (st->storage_id != storage_id)
97 *path = strdup(st->root());
99 _E("Failed to copy the root string : %d", errno);
100 return STORAGE_ERROR_OUT_OF_MEMORY;
102 return STORAGE_ERROR_NONE;
105 /* external storage */
106 ret = storage_ext_get_root(storage_id, root, sizeof(root));
108 _E("Failed to get root path of external storage(%d, %d", storage_id, ret);
109 return STORAGE_ERROR_INVALID_PARAMETER;
112 *path = strdup(root);
114 _E("Failed to copy the root string : %d", errno);
115 return STORAGE_ERROR_OUT_OF_MEMORY;
118 return STORAGE_ERROR_NONE;
121 API int storage_get_directory(int storage_id, storage_directory_e type, char **path)
123 const struct storage_ops *st;
131 if (!path || storage_id < 0) {
132 _E("Invalid parameger");
133 return STORAGE_ERROR_INVALID_PARAMETER;
136 if (type < 0 || type >= STORAGE_DIRECTORY_MAX) {
137 _E("Invalid parameter");
138 return STORAGE_ERROR_INVALID_PARAMETER;
141 /* internal storage */
143 DD_LIST_FOREACH(st_int_head, elem, st) {
144 if (st->storage_id != storage_id)
151 snprintf(root, sizeof(root), "%s", st->root());
152 if (type == STORAGE_DIRECTORY_SYSTEM_RINGTONES) {
153 ret = system_settings_get_value_string(SYSTEM_SETTINGS_KEY_INCOMING_CALL_RINGTONE, &temp2);
155 _E("Failed to get ringtone path : %d", ret);
156 return STORAGE_ERROR_OPERATION_FAILED;
158 end = strrchr(temp2, '/');
161 snprintf(temp, PATH_MAX, "%s", temp2);
164 snprintf(temp, PATH_MAX, "%s/%s", root, dir_path[type]);
169 /* external storage */
170 if (type == STORAGE_DIRECTORY_SYSTEM_RINGTONES) {
171 _E("Not support directory : id(%d) type(%d)", storage_id, type);
172 return STORAGE_ERROR_NOT_SUPPORTED;
175 ret = storage_ext_get_root(storage_id, root, sizeof(root));
177 _E("Failed to get root dir for external storage(id:%d, ret:%d)", storage_id, ret);
178 return STORAGE_ERROR_OPERATION_FAILED;
181 snprintf(temp, sizeof(temp), "%s/%s", root, dir_path[type]);
184 *path = strdup(temp);
186 _E("Failed to copy the directory(%d) string : %d", type, errno);
187 return STORAGE_ERROR_OUT_OF_MEMORY;
190 return STORAGE_ERROR_NONE;
193 API int storage_get_type(int storage_id, storage_type_e *type)
195 const struct storage_ops *st;
198 if (!type || storage_id < 0) {
199 _E("Invalid parameger");
200 return STORAGE_ERROR_INVALID_PARAMETER;
203 /* internal storage */
204 DD_LIST_FOREACH(st_int_head, elem, st) {
205 if (st->storage_id != storage_id)
208 return STORAGE_ERROR_NONE;
211 /* external storage */
212 *type = STORAGE_TYPE_EXTERNAL;
214 return STORAGE_ERROR_NONE;
217 API int storage_get_state(int storage_id, storage_state_e *state)
219 const struct storage_ops *ops;
224 if (!state || storage_id < 0) {
225 _E("Invalid parameger");
226 return STORAGE_ERROR_INVALID_PARAMETER;
229 /* internal storage */
230 DD_LIST_FOREACH(st_int_head, elem, ops) {
231 if (ops->storage_id != storage_id)
233 *state = ops->get_state();
234 return STORAGE_ERROR_NONE;
237 /* external storage */
238 ret = storage_ext_get_state(storage_id, &st);
240 _E("Failed to get state (storage id(%d), ret(%d))", storage_id, ret);
241 return STORAGE_ERROR_OPERATION_FAILED;
245 return STORAGE_ERROR_NONE;
248 API int storage_set_state_changed_cb(int storage_id, storage_state_changed_cb callback, void *user_data)
250 const struct storage_ops *st;
251 struct storage_cb_info info;
256 _E("Invalid parameger");
257 return STORAGE_ERROR_INVALID_PARAMETER;
260 /* Internal storage does not support registering changed callback */
261 DD_LIST_FOREACH(st_int_head, elem, st)
262 if (st->storage_id == storage_id)
263 return STORAGE_ERROR_NONE;
265 /* external storage */
266 info.id = storage_id;
267 info.state_cb = callback;
268 info.user_data = user_data;
270 ret = storage_ext_register_cb(STORAGE_CALLBACK_STATE, &info);
272 _E("Failed to register callback : id(%d)", storage_id);
273 return STORAGE_ERROR_OPERATION_FAILED;
276 return STORAGE_ERROR_NONE;
279 API int storage_unset_state_changed_cb(int storage_id, storage_state_changed_cb callback)
281 const struct storage_ops *st;
282 struct storage_cb_info info;
287 _E("Invalid parameger");
288 return STORAGE_ERROR_INVALID_PARAMETER;
291 /* Internal storage does not support registering changed callback */
292 DD_LIST_FOREACH(st_int_head, elem, st)
293 if (st->storage_id == storage_id)
294 return STORAGE_ERROR_NONE;
296 /* external storage */
297 info.id = storage_id;
298 info.state_cb = callback;
300 ret = storage_ext_unregister_cb(STORAGE_CALLBACK_STATE, &info);
302 _E("Failed to unregister callback : id(%d)", storage_id);
303 return STORAGE_ERROR_OPERATION_FAILED;
306 return STORAGE_ERROR_NONE;
309 API int storage_get_total_space(int storage_id, unsigned long long *bytes)
311 const struct storage_ops *st;
312 unsigned long long total;
316 if (!bytes || storage_id < 0) {
317 _E("Invalid parameger");
318 return STORAGE_ERROR_INVALID_PARAMETER;
321 /* internal storage */
322 DD_LIST_FOREACH(st_int_head, elem, st) {
323 if (st->storage_id != storage_id)
325 ret = st->get_space(&total, NULL);
329 /* external storage */
330 ret = storage_ext_get_space(storage_id, &total, NULL);
334 _E("Failed to get total memory : id(%d)", storage_id);
335 return STORAGE_ERROR_OPERATION_FAILED;
339 return STORAGE_ERROR_NONE;
342 API int storage_get_available_space(int storage_id, unsigned long long *bytes)
344 const struct storage_ops *st;
345 unsigned long long avail;
350 _E("Invalid parameger");
351 return STORAGE_ERROR_INVALID_PARAMETER;
354 /* internal storage */
355 DD_LIST_FOREACH(st_int_head, elem, st) {
356 if (st->storage_id != storage_id)
358 ret = st->get_space(NULL, &avail);
362 /* external storage */
363 ret = storage_ext_get_space(storage_id,NULL, &avail);
367 _E("Failed to get available memory : id(%d)", storage_id);
368 return STORAGE_ERROR_OPERATION_FAILED;
372 return STORAGE_ERROR_NONE;