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;
86 if (!path || storage_id < 0) {
87 _E("Invalid parameger");
88 return STORAGE_ERROR_INVALID_PARAMETER;
91 /* internal storage */
92 DD_LIST_FOREACH(st_int_head, elem, st) {
93 if (st->storage_id != storage_id)
95 *path = strdup(st->root());
97 _E("Failed to copy the root string : %d", errno);
98 return STORAGE_ERROR_OUT_OF_MEMORY;
100 return STORAGE_ERROR_NONE;
103 /* TODO external storage */
105 return STORAGE_ERROR_NONE;
108 API int storage_get_directory(int storage_id, storage_directory_e type, char **path)
110 const struct storage_ops *st;
118 if (!path || storage_id < 0) {
119 _E("Invalid parameger");
120 return STORAGE_ERROR_INVALID_PARAMETER;
123 if (type < 0 || type >= STORAGE_DIRECTORY_MAX) {
124 _E("Invalid parameter");
125 return STORAGE_ERROR_INVALID_PARAMETER;
128 /* internal storage */
130 DD_LIST_FOREACH(st_int_head, elem, st) {
131 if (st->storage_id != storage_id)
138 snprintf(root, sizeof(root), "%s", st->root());
139 if (type == STORAGE_DIRECTORY_SYSTEM_RINGTONES) {
140 ret = system_settings_get_value_string(SYSTEM_SETTINGS_KEY_INCOMING_CALL_RINGTONE, &temp2);
142 _E("Failed to get ringtone path : %d", ret);
143 return STORAGE_ERROR_OPERATION_FAILED;
145 end = strrchr(temp2, '/');
148 snprintf(temp, PATH_MAX, "%s", temp2);
151 snprintf(temp, PATH_MAX, "%s/%s", root, dir_path[type]);
156 /* external storage */
157 return STORAGE_ERROR_NONE;
160 *path = strdup(temp);
162 _E("Failed to copy the directory(%d) string : %d", type, errno);
163 return STORAGE_ERROR_OUT_OF_MEMORY;
166 return STORAGE_ERROR_NONE;
169 API int storage_get_type(int storage_id, storage_type_e *type)
171 const struct storage_ops *st;
174 if (!type || storage_id < 0) {
175 _E("Invalid parameger");
176 return STORAGE_ERROR_INVALID_PARAMETER;
179 /* internal storage */
180 DD_LIST_FOREACH(st_int_head, elem, st) {
181 if (st->storage_id != storage_id)
184 return STORAGE_ERROR_NONE;
187 /* external storage */
189 return STORAGE_ERROR_NONE;
192 API int storage_get_state(int storage_id, storage_state_e *state)
194 const struct storage_ops *ops;
198 _E("Invalid parameger");
199 return STORAGE_ERROR_INVALID_PARAMETER;
202 /* internal storage */
203 DD_LIST_FOREACH(st_int_head, elem, ops) {
204 if (ops->storage_id != storage_id)
206 *state = ops->get_state();
207 return STORAGE_ERROR_NONE;
210 /* external storage */
212 return STORAGE_ERROR_NONE;
215 API int storage_set_state_changed_cb(int storage_id, storage_state_changed_cb callback, void *user_data)
217 const struct storage_ops *st;
221 _E("Invalid parameger");
222 return STORAGE_ERROR_INVALID_PARAMETER;
225 /* Internal storage does not support registering changed callback */
226 DD_LIST_FOREACH(st_int_head, elem, st)
227 if (st->storage_id == storage_id)
228 return STORAGE_ERROR_NONE;
230 /* external storage */
232 return STORAGE_ERROR_NONE;
235 API int storage_unset_state_changed_cb(int storage_id, storage_state_changed_cb callback)
237 const struct storage_ops *st;
241 _E("Invalid parameger");
242 return STORAGE_ERROR_INVALID_PARAMETER;
245 /* Internal storage does not support registering changed callback */
246 DD_LIST_FOREACH(st_int_head, elem, st)
247 if (st->storage_id == storage_id)
248 return STORAGE_ERROR_NONE;
250 return STORAGE_ERROR_NONE;
253 API int storage_get_total_space(int storage_id, unsigned long long *bytes)
255 const struct storage_ops *st;
256 unsigned long long total;
260 if (!bytes || storage_id < 0) {
261 _E("Invalid parameger");
262 return STORAGE_ERROR_INVALID_PARAMETER;
265 /* internal storage */
266 DD_LIST_FOREACH(st_int_head, elem, st) {
267 if (st->storage_id != storage_id)
269 ret = st->get_space(&total, NULL);
273 /* external storage */
278 _E("Failed to get total memory : id(%d)", storage_id);
279 return STORAGE_ERROR_OPERATION_FAILED;
283 return STORAGE_ERROR_NONE;
286 API int storage_get_available_space(int storage_id, unsigned long long *bytes)
288 const struct storage_ops *st;
289 unsigned long long avail;
294 _E("Invalid parameger");
295 return STORAGE_ERROR_INVALID_PARAMETER;
298 /* internal storage */
299 DD_LIST_FOREACH(st_int_head, elem, st) {
300 if (st->storage_id != storage_id)
302 ret = st->get_space(NULL, &avail);
306 /* external storage */
311 _E("Failed to get available memory : id(%d)", storage_id);
312 return STORAGE_ERROR_OPERATION_FAILED;
316 return STORAGE_ERROR_NONE;