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 return STORAGE_ERROR_NONE;
173 *path = strdup(temp);
175 _E("Failed to copy the directory(%d) string : %d", type, errno);
176 return STORAGE_ERROR_OUT_OF_MEMORY;
179 return STORAGE_ERROR_NONE;
182 API int storage_get_type(int storage_id, storage_type_e *type)
184 const struct storage_ops *st;
187 if (!type || storage_id < 0) {
188 _E("Invalid parameger");
189 return STORAGE_ERROR_INVALID_PARAMETER;
192 /* internal storage */
193 DD_LIST_FOREACH(st_int_head, elem, st) {
194 if (st->storage_id != storage_id)
197 return STORAGE_ERROR_NONE;
200 /* external storage */
202 return STORAGE_ERROR_NONE;
205 API int storage_get_state(int storage_id, storage_state_e *state)
207 const struct storage_ops *ops;
211 _E("Invalid parameger");
212 return STORAGE_ERROR_INVALID_PARAMETER;
215 /* internal storage */
216 DD_LIST_FOREACH(st_int_head, elem, ops) {
217 if (ops->storage_id != storage_id)
219 *state = ops->get_state();
220 return STORAGE_ERROR_NONE;
223 /* external storage */
225 return STORAGE_ERROR_NONE;
228 API int storage_set_state_changed_cb(int storage_id, storage_state_changed_cb callback, void *user_data)
230 const struct storage_ops *st;
231 struct storage_cb_info info;
236 _E("Invalid parameger");
237 return STORAGE_ERROR_INVALID_PARAMETER;
240 /* Internal storage does not support registering changed callback */
241 DD_LIST_FOREACH(st_int_head, elem, st)
242 if (st->storage_id == storage_id)
243 return STORAGE_ERROR_NONE;
245 /* external storage */
246 info.id = storage_id;
247 info.state_cb = callback;
248 info.user_data = user_data;
250 ret = storage_ext_register_cb(STORAGE_CALLBACK_STATE, &info);
252 _E("Failed to register callback : id(%d)", storage_id);
253 return STORAGE_ERROR_OPERATION_FAILED;
256 return STORAGE_ERROR_NONE;
259 API int storage_unset_state_changed_cb(int storage_id, storage_state_changed_cb callback)
261 const struct storage_ops *st;
262 struct storage_cb_info info;
267 _E("Invalid parameger");
268 return STORAGE_ERROR_INVALID_PARAMETER;
271 /* Internal storage does not support registering changed callback */
272 DD_LIST_FOREACH(st_int_head, elem, st)
273 if (st->storage_id == storage_id)
274 return STORAGE_ERROR_NONE;
276 /* external storage */
277 info.id = storage_id;
278 info.state_cb = callback;
280 ret = storage_ext_unregister_cb(STORAGE_CALLBACK_STATE, &info);
282 _E("Failed to unregister callback : id(%d)", storage_id);
283 return STORAGE_ERROR_OPERATION_FAILED;
286 return STORAGE_ERROR_NONE;
289 API int storage_get_total_space(int storage_id, unsigned long long *bytes)
291 const struct storage_ops *st;
292 unsigned long long total;
296 if (!bytes || storage_id < 0) {
297 _E("Invalid parameger");
298 return STORAGE_ERROR_INVALID_PARAMETER;
301 /* internal storage */
302 DD_LIST_FOREACH(st_int_head, elem, st) {
303 if (st->storage_id != storage_id)
305 ret = st->get_space(&total, NULL);
309 /* external storage */
314 _E("Failed to get total memory : id(%d)", storage_id);
315 return STORAGE_ERROR_OPERATION_FAILED;
319 return STORAGE_ERROR_NONE;
322 API int storage_get_available_space(int storage_id, unsigned long long *bytes)
324 const struct storage_ops *st;
325 unsigned long long avail;
330 _E("Invalid parameger");
331 return STORAGE_ERROR_INVALID_PARAMETER;
334 /* internal storage */
335 DD_LIST_FOREACH(st_int_head, elem, st) {
336 if (st->storage_id != storage_id)
338 ret = st->get_space(NULL, &avail);
342 /* external storage */
347 _E("Failed to get available memory : id(%d)", storage_id);
348 return STORAGE_ERROR_OPERATION_FAILED;
352 return STORAGE_ERROR_NONE;