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); //LCOV_EXCL_LINE
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;
89 return STORAGE_ERROR_NOT_SUPPORTED;
92 _E("Invalid parameger");
93 return STORAGE_ERROR_INVALID_PARAMETER;
96 /* internal storage */
97 DD_LIST_FOREACH(st_int_head, elem, st) {
98 if (st->storage_id != storage_id)
100 *path = strdup(st->root());
102 //LCOV_EXCL_START System Error
103 _E("Failed to copy the root string : %d", errno);
104 return STORAGE_ERROR_OUT_OF_MEMORY;
107 return STORAGE_ERROR_NONE;
110 /* external storage */
111 ret = storage_ext_get_root(storage_id, root, sizeof(root));
113 _E("Failed to get root path of external storage(%d, %d", storage_id, ret); //LCOV_EXCL_LINE
114 return STORAGE_ERROR_INVALID_PARAMETER;
117 *path = strdup(root);
119 _E("Failed to copy the root string : %d", errno);
120 return STORAGE_ERROR_OUT_OF_MEMORY;
123 return STORAGE_ERROR_NONE;
126 API int storage_get_directory(int storage_id, storage_directory_e type, char **path)
128 const struct storage_ops *st;
137 return STORAGE_ERROR_NOT_SUPPORTED;
140 _E("Invalid parameger");
141 return STORAGE_ERROR_INVALID_PARAMETER;
144 if (type < 0 || type >= STORAGE_DIRECTORY_MAX) {
145 _E("Invalid parameter");
146 return STORAGE_ERROR_INVALID_PARAMETER;
149 /* internal storage */
151 DD_LIST_FOREACH(st_int_head, elem, st) {
152 if (st->storage_id != storage_id)
159 snprintf(root, sizeof(root), "%s", st->root());
160 if (type == STORAGE_DIRECTORY_SYSTEM_RINGTONES) {
161 ret = system_settings_get_value_string(SYSTEM_SETTINGS_KEY_INCOMING_CALL_RINGTONE, &temp2);
163 _E("Failed to get ringtone path : %d", ret); //LCOV_EXCL_LINE
164 return STORAGE_ERROR_OPERATION_FAILED;
166 end = strrchr(temp2, '/');
169 snprintf(temp, PATH_MAX, "%s", temp2);
172 snprintf(temp, PATH_MAX, "%s/%s", root, dir_path[type]);
177 /* external storage */
178 if (type == STORAGE_DIRECTORY_SYSTEM_RINGTONES) {
179 _E("Not support directory : id(%d) type(%d)", storage_id, type); //LCOV_EXCL_LINE
180 return STORAGE_ERROR_NOT_SUPPORTED;
183 ret = storage_ext_get_root(storage_id, root, sizeof(root));
185 _E("Failed to get root dir for external storage(id:%d, ret:%d)", storage_id, ret); //LCOV_EXCL_LINE
186 return STORAGE_ERROR_OPERATION_FAILED;
189 snprintf(temp, sizeof(temp), "%s/%s", root, dir_path[type]);
192 *path = strdup(temp);
194 _E("Failed to copy the directory(%d) string : %d", type, errno); //LCOV_EXCL_LINE
195 return STORAGE_ERROR_OUT_OF_MEMORY;
198 return STORAGE_ERROR_NONE;
201 API int storage_get_type(int storage_id, storage_type_e *type)
203 const struct storage_ops *st;
207 return STORAGE_ERROR_NOT_SUPPORTED;
210 _E("Invalid parameger");
211 return STORAGE_ERROR_INVALID_PARAMETER;
214 /* internal storage */
215 DD_LIST_FOREACH(st_int_head, elem, st) {
216 if (st->storage_id != storage_id)
219 return STORAGE_ERROR_NONE;
222 /* external storage */
223 *type = STORAGE_TYPE_EXTERNAL;
225 return STORAGE_ERROR_NONE;
228 API int storage_get_state(int storage_id, storage_state_e *state)
230 const struct storage_ops *ops;
236 return STORAGE_ERROR_NOT_SUPPORTED;
239 _E("Invalid parameger");
240 return STORAGE_ERROR_INVALID_PARAMETER;
243 /* internal storage */
244 DD_LIST_FOREACH(st_int_head, elem, ops) {
245 if (ops->storage_id != storage_id)
247 *state = ops->get_state();
248 return STORAGE_ERROR_NONE;
251 /* external storage */
252 ret = storage_ext_get_state(storage_id, &st);
254 _E("Failed to get state (storage id(%d), ret(%d))", storage_id, ret); //LCOV_EXCL_LINE
255 return STORAGE_ERROR_OPERATION_FAILED;
259 return STORAGE_ERROR_NONE;
262 API int storage_set_state_changed_cb(int storage_id, storage_state_changed_cb callback, void *user_data)
264 const struct storage_ops *st;
265 struct storage_cb_info info;
270 return STORAGE_ERROR_NOT_SUPPORTED;
273 _E("Invalid parameger");
274 return STORAGE_ERROR_INVALID_PARAMETER;
277 /* Internal storage does not support registering changed callback */
278 DD_LIST_FOREACH(st_int_head, elem, st)
279 if (st->storage_id == storage_id)
280 return STORAGE_ERROR_NONE;
282 /* external storage */
283 info.id = storage_id;
284 info.state_cb = callback;
285 info.user_data = user_data;
287 ret = storage_ext_register_cb(STORAGE_CALLBACK_ID, &info);
289 _E("Failed to register callback : id(%d)", storage_id); //LCOV_EXCL_LINE
290 return STORAGE_ERROR_OPERATION_FAILED;
293 return STORAGE_ERROR_NONE;
296 API int storage_unset_state_changed_cb(int storage_id, storage_state_changed_cb callback)
298 const struct storage_ops *st;
299 struct storage_cb_info info;
304 return STORAGE_ERROR_NOT_SUPPORTED;
307 _E("Invalid parameger");
308 return STORAGE_ERROR_INVALID_PARAMETER;
311 /* Internal storage does not support registering changed callback */
312 DD_LIST_FOREACH(st_int_head, elem, st)
313 if (st->storage_id == storage_id)
314 return STORAGE_ERROR_NONE;
316 /* external storage */
317 info.id = storage_id;
318 info.state_cb = callback;
320 ret = storage_ext_unregister_cb(STORAGE_CALLBACK_ID, &info);
322 _E("Failed to unregister callback : id(%d)", storage_id); //LCOV_EXCL_LINE
323 return STORAGE_ERROR_OPERATION_FAILED;
326 return STORAGE_ERROR_NONE;
329 API int storage_get_total_space(int storage_id, unsigned long long *bytes)
331 const struct storage_ops *st;
332 unsigned long long total;
337 return STORAGE_ERROR_NOT_SUPPORTED;
340 _E("Invalid parameger");
341 return STORAGE_ERROR_INVALID_PARAMETER;
344 /* internal storage */
345 DD_LIST_FOREACH(st_int_head, elem, st) {
346 if (st->storage_id != storage_id)
348 ret = st->get_space(&total, NULL);
352 /* external storage */
353 ret = storage_ext_get_space(storage_id, &total, NULL);
357 _E("Failed to get total memory : id(%d)", storage_id); //LCOV_EXCL_LINE
359 return STORAGE_ERROR_NOT_SUPPORTED;
360 return STORAGE_ERROR_OPERATION_FAILED;
364 return STORAGE_ERROR_NONE;
367 API int storage_get_available_space(int storage_id, unsigned long long *bytes)
369 const struct storage_ops *st;
370 unsigned long long avail;
375 return STORAGE_ERROR_NOT_SUPPORTED;
378 _E("Invalid parameger");
379 return STORAGE_ERROR_INVALID_PARAMETER;
382 /* internal storage */
383 DD_LIST_FOREACH(st_int_head, elem, st) {
384 if (st->storage_id != storage_id)
386 ret = st->get_space(NULL, &avail);
390 /* external storage */
391 ret = storage_ext_get_space(storage_id, NULL, &avail);
395 _E("Failed to get available memory : id(%d)", storage_id); //LCOV_EXCL_LINE
397 return STORAGE_ERROR_NOT_SUPPORTED;
398 return STORAGE_ERROR_OPERATION_FAILED;
402 return STORAGE_ERROR_NONE;
405 API int storage_set_changed_cb(storage_type_e type, storage_changed_cb callback, void *user_data)
408 struct storage_cb_info info;
410 if (type == STORAGE_TYPE_INTERNAL) {
411 _E("Internal storage is not supported");
412 return STORAGE_ERROR_NOT_SUPPORTED;
415 if (type != STORAGE_TYPE_EXTERNAL) {
416 _E("Invalid type (%d)", type);
417 return STORAGE_ERROR_INVALID_PARAMETER;
421 _E("Callback is NULL");
422 return STORAGE_ERROR_INVALID_PARAMETER;
425 /* external storage */
427 info.type_cb = callback;
428 info.user_data = user_data;
430 ret = storage_ext_register_cb(STORAGE_CALLBACK_TYPE, &info);
432 _E("Failed to register storage callback(ret:%d)", ret); //LCOV_EXCL_LINE
433 return STORAGE_ERROR_OPERATION_FAILED;
436 return STORAGE_ERROR_NONE;
439 API int storage_unset_changed_cb(storage_type_e type, storage_changed_cb callback)
441 struct storage_cb_info info;
444 if (type == STORAGE_TYPE_INTERNAL) {
445 _E("Internal storage is not supported");
446 return STORAGE_ERROR_NOT_SUPPORTED;
449 if (type != STORAGE_TYPE_EXTERNAL) {
450 _E("Invalid type (%d)", type);
451 return STORAGE_ERROR_INVALID_PARAMETER;
455 _E("Callback is NULL");
456 return STORAGE_ERROR_INVALID_PARAMETER;
459 /* external storage */
461 info.type_cb = callback;
463 ret = storage_ext_unregister_cb(STORAGE_CALLBACK_TYPE, &info);
465 _E("Failed to unregister storage callback(ret:%d)", ret); //LCOV_EXCL_LINE
466 return STORAGE_ERROR_OPERATION_FAILED;
469 return STORAGE_ERROR_NONE;