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.
27 const char *dir_path[STORAGE_DIRECTORY_MAX] = {
28 [STORAGE_DIRECTORY_IMAGES] = "Images",
29 [STORAGE_DIRECTORY_SOUNDS] = "Sounds",
30 [STORAGE_DIRECTORY_VIDEOS] = "Videos",
31 [STORAGE_DIRECTORY_CAMERA] = "DCIM",
32 [STORAGE_DIRECTORY_DOWNLOADS] = "Downloads",
33 [STORAGE_DIRECTORY_MUSIC] = "Music",
34 [STORAGE_DIRECTORY_DOCUMENTS] = "Documents",
35 [STORAGE_DIRECTORY_OTHERS] = "Others",
36 [STORAGE_DIRECTORY_SYSTEM_RINGTONES] = "/opt/usr/share/settings/Ringtones",
39 static dd_list *st_head;
41 void add_device(const struct storage_ops *st)
43 DD_LIST_APPEND(st_head, st);
46 void remove_device(const struct storage_ops *st)
48 DD_LIST_REMOVE(st_head, st);
51 API int storage_foreach_device_supported(storage_device_supported_cb callback, void *user_data)
53 const struct storage_ops *st;
55 int storage_id = 0, ret;
58 _E("Invalid parameter");
59 return STORAGE_ERROR_INVALID_PARAMETER;
62 DD_LIST_FOREACH(st_head, elem, st) {
63 ret = callback(storage_id, st->type, st->get_state(),
64 st->root(), user_data);
65 /* if the return value is false, will be stop to iterate */
71 return STORAGE_ERROR_NONE;
74 API int storage_get_root_directory(int storage_id, char **path)
76 const struct storage_ops *st;
79 _E("Invalid parameger");
80 return STORAGE_ERROR_INVALID_PARAMETER;
83 st = DD_LIST_NTH(st_head, storage_id);
85 _E("Not supported storage : id(%d)", storage_id);
86 return STORAGE_ERROR_NOT_SUPPORTED;
89 *path = strdup(st->root());
91 _E("Failed to copy the root string : %d", errno);
92 return STORAGE_ERROR_OUT_OF_MEMORY;
95 return STORAGE_ERROR_NONE;
98 API int storage_get_directory(int storage_id, storage_directory_e type, char **path)
100 const struct storage_ops *st;
105 _E("Invalid parameger");
106 return STORAGE_ERROR_INVALID_PARAMETER;
109 if (type < 0 || type >= STORAGE_DIRECTORY_MAX) {
110 _E("Invalid parameter");
111 return STORAGE_ERROR_INVALID_PARAMETER;
114 st = DD_LIST_NTH(st_head, storage_id);
116 _E("Not supported storage : id(%d)", storage_id);
117 return STORAGE_ERROR_NOT_SUPPORTED;
120 if (st->type != STORAGE_TYPE_INTERNAL
121 && type == STORAGE_DIRECTORY_SYSTEM_RINGTONES) {
122 _E("Not support directory : id(%d) type(%d)", storage_id, type);
123 return STORAGE_ERROR_NOT_SUPPORTED;
127 if (type == STORAGE_DIRECTORY_SYSTEM_RINGTONES)
128 snprintf(temp, PATH_MAX, "%s", dir_path[type]);
130 snprintf(temp, PATH_MAX, "%s/%s", root, dir_path[type]);
132 *path = strdup(temp);
134 _E("Failed to copy the directory(%d) string : %d", type, errno);
135 return STORAGE_ERROR_OUT_OF_MEMORY;
138 return STORAGE_ERROR_NONE;
141 API int storage_get_type(int storage_id, storage_type_e *type)
143 const struct storage_ops *st;
146 _E("Invalid parameger");
147 return STORAGE_ERROR_INVALID_PARAMETER;
150 st = DD_LIST_NTH(st_head, storage_id);
152 _E("Not supported storage : id(%d)", storage_id);
153 return STORAGE_ERROR_NOT_SUPPORTED;
158 return STORAGE_ERROR_NONE;
161 API int storage_get_state(int storage_id, storage_state_e *state)
163 const struct storage_ops *st;
166 _E("Invalid parameger");
167 return STORAGE_ERROR_INVALID_PARAMETER;
170 st = DD_LIST_NTH(st_head, storage_id);
172 _E("Not supported storage : id(%d)", storage_id);
173 return STORAGE_ERROR_NOT_SUPPORTED;
176 *state = st->get_state();
178 return STORAGE_ERROR_NONE;
181 API int storage_set_state_changed_cb(int storage_id, storage_state_changed_cb callback, void *user_data)
183 const struct storage_ops *st;
184 struct storage_cb_info info;
188 _E("Invalid parameger");
189 return STORAGE_ERROR_INVALID_PARAMETER;
192 st = DD_LIST_NTH(st_head, storage_id);
194 _E("Not supported storage : id(%d)", storage_id);
195 return STORAGE_ERROR_NOT_SUPPORTED;
198 /* do not register changed callback in case of internal memory */
199 if (st->type == STORAGE_TYPE_INTERNAL)
200 return STORAGE_ERROR_NONE;
202 info.id = storage_id;
203 info.state_cb = callback;
204 info.user_data = user_data;
206 ret = st->register_cb(STORAGE_CALLBACK_STATE, &info);
208 _E("Failed to register callback : id(%d)", storage_id);
209 return STORAGE_ERROR_OPERATION_FAILED;
212 return STORAGE_ERROR_NONE;
215 API int storage_unset_state_changed_cb(int storage_id, storage_state_changed_cb callback)
217 const struct storage_ops *st;
218 struct storage_cb_info info;
222 _E("Invalid parameger");
223 return STORAGE_ERROR_INVALID_PARAMETER;
226 st = DD_LIST_NTH(st_head, storage_id);
228 _E("Not supported storage : id(%d)", storage_id);
229 return STORAGE_ERROR_NOT_SUPPORTED;
232 /* in case of internal memory, it does not register changed callback */
233 if (st->type == STORAGE_TYPE_INTERNAL)
234 return STORAGE_ERROR_NONE;
236 info.id = storage_id;
237 info.state_cb = callback;
239 ret = st->unregister_cb(STORAGE_CALLBACK_STATE, &info);
241 _E("Failed to unregister callback : id(%d)", storage_id);
242 return STORAGE_ERROR_OPERATION_FAILED;
245 return STORAGE_ERROR_NONE;
248 API int storage_get_total_space(int storage_id, unsigned long long *bytes)
250 const struct storage_ops *st;
251 unsigned long long total;
255 _E("Invalid parameger");
256 return STORAGE_ERROR_INVALID_PARAMETER;
259 st = DD_LIST_NTH(st_head, storage_id);
261 _E("Not supported storage : id(%d)", storage_id);
262 return STORAGE_ERROR_NOT_SUPPORTED;
265 ret = st->get_space(&total, NULL);
267 _E("Failed to get total memory : id(%d)", storage_id);
268 return STORAGE_ERROR_OPERATION_FAILED;
273 return STORAGE_ERROR_NONE;
276 API int storage_get_available_space(int storage_id, unsigned long long *bytes)
278 const struct storage_ops *st;
279 unsigned long long avail;
283 _E("Invalid parameger");
284 return STORAGE_ERROR_INVALID_PARAMETER;
287 st = DD_LIST_NTH(st_head, storage_id);
289 _E("Not supported storage : id(%d)", storage_id);
290 return STORAGE_ERROR_NOT_SUPPORTED;
293 ret = st->get_space(NULL, &avail);
295 _E("Failed to get available memory : id(%d)", storage_id);
296 return STORAGE_ERROR_OPERATION_FAILED;
301 return STORAGE_ERROR_NONE;