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;
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 _E("Failed to copy the root string : %d", errno);
103 return STORAGE_ERROR_OUT_OF_MEMORY;
105 return STORAGE_ERROR_NONE;
108 /* external storage */
109 ret = storage_ext_get_root(storage_id, root, sizeof(root));
111 _E("Failed to get root path of external storage(%d, %d", storage_id, ret);
112 return STORAGE_ERROR_INVALID_PARAMETER;
115 *path = strdup(root);
117 _E("Failed to copy the root string : %d", errno);
118 return STORAGE_ERROR_OUT_OF_MEMORY;
121 return STORAGE_ERROR_NONE;
124 API int storage_get_directory(int storage_id, storage_directory_e type, char **path)
126 const struct storage_ops *st;
135 return STORAGE_ERROR_NOT_SUPPORTED;
138 _E("Invalid parameger");
139 return STORAGE_ERROR_INVALID_PARAMETER;
142 if (type < 0 || type >= STORAGE_DIRECTORY_MAX) {
143 _E("Invalid parameter");
144 return STORAGE_ERROR_INVALID_PARAMETER;
147 /* internal storage */
149 DD_LIST_FOREACH(st_int_head, elem, st) {
150 if (st->storage_id != storage_id)
157 snprintf(root, sizeof(root), "%s", st->root());
158 if (type == STORAGE_DIRECTORY_SYSTEM_RINGTONES) {
159 ret = system_settings_get_value_string(SYSTEM_SETTINGS_KEY_INCOMING_CALL_RINGTONE, &temp2);
161 _E("Failed to get ringtone path : %d", ret);
162 return STORAGE_ERROR_OPERATION_FAILED;
164 end = strrchr(temp2, '/');
167 snprintf(temp, PATH_MAX, "%s", temp2);
170 snprintf(temp, PATH_MAX, "%s/%s", root, dir_path[type]);
175 /* external storage */
176 if (type == STORAGE_DIRECTORY_SYSTEM_RINGTONES) {
177 _E("Not support directory : id(%d) type(%d)", storage_id, type);
178 return STORAGE_ERROR_NOT_SUPPORTED;
181 ret = storage_ext_get_root(storage_id, root, sizeof(root));
183 _E("Failed to get root dir for external storage(id:%d, ret:%d)", storage_id, ret);
184 return STORAGE_ERROR_OPERATION_FAILED;
187 snprintf(temp, sizeof(temp), "%s/%s", root, dir_path[type]);
190 *path = strdup(temp);
192 _E("Failed to copy the directory(%d) string : %d", type, errno);
193 return STORAGE_ERROR_OUT_OF_MEMORY;
196 return STORAGE_ERROR_NONE;
199 API int storage_get_type(int storage_id, storage_type_e *type)
201 const struct storage_ops *st;
205 return STORAGE_ERROR_NOT_SUPPORTED;
208 _E("Invalid parameger");
209 return STORAGE_ERROR_INVALID_PARAMETER;
212 /* internal storage */
213 DD_LIST_FOREACH(st_int_head, elem, st) {
214 if (st->storage_id != storage_id)
217 return STORAGE_ERROR_NONE;
220 /* external storage */
221 *type = STORAGE_TYPE_EXTERNAL;
223 return STORAGE_ERROR_NONE;
226 API int storage_get_state(int storage_id, storage_state_e *state)
228 const struct storage_ops *ops;
234 return STORAGE_ERROR_NOT_SUPPORTED;
237 _E("Invalid parameger");
238 return STORAGE_ERROR_INVALID_PARAMETER;
241 /* internal storage */
242 DD_LIST_FOREACH(st_int_head, elem, ops) {
243 if (ops->storage_id != storage_id)
245 *state = ops->get_state();
246 return STORAGE_ERROR_NONE;
249 /* external storage */
250 ret = storage_ext_get_state(storage_id, &st);
252 _E("Failed to get state (storage id(%d), ret(%d))", storage_id, ret);
253 return STORAGE_ERROR_OPERATION_FAILED;
257 return STORAGE_ERROR_NONE;
260 API int storage_set_state_changed_cb(int storage_id, storage_state_changed_cb callback, void *user_data)
262 const struct storage_ops *st;
263 struct storage_cb_info info;
268 return STORAGE_ERROR_NOT_SUPPORTED;
271 _E("Invalid parameger");
272 return STORAGE_ERROR_INVALID_PARAMETER;
275 /* Internal storage does not support registering changed callback */
276 DD_LIST_FOREACH(st_int_head, elem, st)
277 if (st->storage_id == storage_id)
278 return STORAGE_ERROR_NONE;
280 /* external storage */
281 info.id = storage_id;
282 info.state_cb = callback;
283 info.user_data = user_data;
285 ret = storage_ext_register_cb(STORAGE_CALLBACK_STATE, &info);
287 _E("Failed to register callback : id(%d)", storage_id);
288 return STORAGE_ERROR_OPERATION_FAILED;
291 return STORAGE_ERROR_NONE;
294 API int storage_unset_state_changed_cb(int storage_id, storage_state_changed_cb callback)
296 const struct storage_ops *st;
297 struct storage_cb_info info;
302 return STORAGE_ERROR_NOT_SUPPORTED;
305 _E("Invalid parameger");
306 return STORAGE_ERROR_INVALID_PARAMETER;
309 /* Internal storage does not support registering changed callback */
310 DD_LIST_FOREACH(st_int_head, elem, st)
311 if (st->storage_id == storage_id)
312 return STORAGE_ERROR_NONE;
314 /* external storage */
315 info.id = storage_id;
316 info.state_cb = callback;
318 ret = storage_ext_unregister_cb(STORAGE_CALLBACK_STATE, &info);
320 _E("Failed to unregister callback : id(%d)", storage_id);
321 return STORAGE_ERROR_OPERATION_FAILED;
324 return STORAGE_ERROR_NONE;
327 API int storage_get_total_space(int storage_id, unsigned long long *bytes)
329 const struct storage_ops *st;
330 unsigned long long total;
335 return STORAGE_ERROR_NOT_SUPPORTED;
338 _E("Invalid parameger");
339 return STORAGE_ERROR_INVALID_PARAMETER;
342 /* internal storage */
343 DD_LIST_FOREACH(st_int_head, elem, st) {
344 if (st->storage_id != storage_id)
346 ret = st->get_space(&total, NULL);
350 /* external storage */
351 ret = storage_ext_get_space(storage_id, &total, NULL);
355 _E("Failed to get total memory : id(%d)", storage_id);
357 return STORAGE_ERROR_NOT_SUPPORTED;
358 return STORAGE_ERROR_OPERATION_FAILED;
362 return STORAGE_ERROR_NONE;
365 API int storage_get_available_space(int storage_id, unsigned long long *bytes)
367 const struct storage_ops *st;
368 unsigned long long avail;
373 return STORAGE_ERROR_NOT_SUPPORTED;
376 _E("Invalid parameger");
377 return STORAGE_ERROR_INVALID_PARAMETER;
380 /* internal storage */
381 DD_LIST_FOREACH(st_int_head, elem, st) {
382 if (st->storage_id != storage_id)
384 ret = st->get_space(NULL, &avail);
388 /* external storage */
389 ret = storage_ext_get_space(storage_id,NULL, &avail);
393 _E("Failed to get available memory : id(%d)", storage_id);
395 return STORAGE_ERROR_NOT_SUPPORTED;
396 return STORAGE_ERROR_OPERATION_FAILED;
400 return STORAGE_ERROR_NONE;