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.
23 #include <tzplatform_config.h>
28 #include "storage-external.h"
30 #define USER_UID_START 5000
32 const char *dir_path[STORAGE_DIRECTORY_MAX];
34 const int tz_id[STORAGE_DIRECTORY_MAX] = {
35 [STORAGE_DIRECTORY_IMAGES] = TZ_USER_IMAGES,
36 [STORAGE_DIRECTORY_SOUNDS] = TZ_USER_SOUNDS,
37 [STORAGE_DIRECTORY_VIDEOS] = TZ_USER_VIDEOS,
38 [STORAGE_DIRECTORY_CAMERA] = TZ_USER_CAMERA,
39 [STORAGE_DIRECTORY_DOWNLOADS] = TZ_USER_DOWNLOADS,
40 [STORAGE_DIRECTORY_MUSIC] = TZ_USER_MUSIC,
41 [STORAGE_DIRECTORY_DOCUMENTS] = TZ_USER_DOCUMENTS,
42 [STORAGE_DIRECTORY_OTHERS] = TZ_USER_OTHERS,
45 static dd_list *st_int_head; /* Internal storage list */
47 static dd_list *compat_cb_list;
48 struct compat_cb_info {
49 storage_state_changed_cb user_cb;
53 void add_device(const struct storage_ops *st)
55 DD_LIST_APPEND(st_int_head, st);
58 void remove_device(const struct storage_ops *st)
60 DD_LIST_REMOVE(st_int_head, st);
63 API int storage_foreach_device_supported(storage_device_supported_cb callback, void *user_data)
65 const struct storage_ops *st;
71 _E("Invalid parameter");
72 return STORAGE_ERROR_INVALID_PARAMETER;
75 if (getuid() <= USER_UID_START)
78 DD_LIST_FOREACH(st_int_head, elem, st) {
80 ret = callback(st->storage_id, st->type, st->get_state(),
81 st->root(), user_data);
82 /* if the return value is false, will be stop to iterate */
88 if (!storage_ext_is_supported()) {
89 _D("Block module is not enabled");
90 return STORAGE_ERROR_NONE;
93 ret = storage_ext_foreach_device_list(callback, user_data);
95 _E("Failed to iterate external devices (%d)", ret); //LCOV_EXCL_LINE
96 return STORAGE_ERROR_OPERATION_FAILED;
99 return STORAGE_ERROR_NONE;
102 API int storage_get_root_directory(int storage_id, char **path)
104 const struct storage_ops *st;
112 return STORAGE_ERROR_NOT_SUPPORTED;
115 _E("Invalid parameger");
116 return STORAGE_ERROR_INVALID_PARAMETER;
119 if (getuid() <= USER_UID_START)
122 /* internal storage */
123 DD_LIST_FOREACH(st_int_head, elem, st) {
124 if (st->storage_id != storage_id)
127 _E("Only apps and user session daemons are allowed "
128 "to use storage_get_root_directory(INTERNAL_STORAGE_ID, ...)");
129 return STORAGE_ERROR_INVALID_PARAMETER;
132 *path = strdup(st->root());
134 //LCOV_EXCL_START System Error
135 _E("Failed to copy the root string : %d", errno);
136 return STORAGE_ERROR_OUT_OF_MEMORY;
139 return STORAGE_ERROR_NONE;
142 /* external storage */
143 ret = storage_ext_get_root(storage_id, root, sizeof(root), &extendedint);
145 _E("Failed to get root path of external storage(%d, %d", storage_id, ret); //LCOV_EXCL_LINE
146 return STORAGE_ERROR_INVALID_PARAMETER;
149 *path = strdup(root);
151 _E("Failed to copy the root string : %d", errno); //LCOV_EXCL_LINE System Error
152 return STORAGE_ERROR_OUT_OF_MEMORY;
155 return STORAGE_ERROR_NONE;
158 API int storage_get_directory(int storage_id, storage_directory_e type, char **path)
160 const struct storage_ops *st;
171 return STORAGE_ERROR_NOT_SUPPORTED;
174 _E("Invalid parameger");
175 return STORAGE_ERROR_INVALID_PARAMETER;
178 if (type < 0 || type >= STORAGE_DIRECTORY_MAX) {
179 _E("Invalid parameter");
180 return STORAGE_ERROR_INVALID_PARAMETER;
183 /* internal storage */
185 DD_LIST_FOREACH(st_int_head, elem, st) {
186 if (st->storage_id != storage_id)
192 if (getuid() <= USER_UID_START)
197 _E("Only apps and user session daemons are allowed "
198 "to use storage_get_root_directory(INTERNAL_STORAGE_ID, ...)");
200 return STORAGE_ERROR_INVALID_PARAMETER;
203 snprintf(root, sizeof(root), "%s", st->root());
204 if (type == STORAGE_DIRECTORY_SYSTEM_RINGTONES) {
205 temp2 = vconf_get_str(VCONFKEY_SETAPPL_CALL_RINGTONE_PATH_STR);
207 return STORAGE_ERROR_OPERATION_FAILED;
208 end = strrchr(temp2, '/');
211 snprintf(temp, PATH_MAX, "%s", temp2);
214 snprintf(temp, PATH_MAX, "%s/%s", root, dir_path[type]);
219 /* external storage */
220 if (type == STORAGE_DIRECTORY_SYSTEM_RINGTONES) {
221 _E("Not support directory : id(%d) type(%d)", storage_id, type); //LCOV_EXCL_LINE
222 return STORAGE_ERROR_NOT_SUPPORTED;
225 ret = storage_ext_get_root(storage_id, root, sizeof(root), &extendedint);
227 _E("Failed to get root dir for external storage(id:%d, ret:%d)", storage_id, ret); //LCOV_EXCL_LINE
228 return STORAGE_ERROR_NOT_SUPPORTED;
230 /* The operation is not decided */
232 return STORAGE_ERROR_NOT_SUPPORTED;
234 snprintf(temp, sizeof(temp), "%s/%s", root, dir_path[type]);
237 *path = strdup(temp);
239 _E("Failed to copy the directory(%d) string : %d", type, errno); //LCOV_EXCL_LINE
240 return STORAGE_ERROR_OUT_OF_MEMORY;
243 return STORAGE_ERROR_NONE;
246 API int storage_get_type(int storage_id, storage_type_e *type)
248 const struct storage_ops *st;
255 return STORAGE_ERROR_NOT_SUPPORTED;
258 _E("Invalid parameger");
259 return STORAGE_ERROR_INVALID_PARAMETER;
262 /* internal storage */
263 DD_LIST_FOREACH(st_int_head, elem, st) {
264 if (st->storage_id != storage_id)
267 return STORAGE_ERROR_NONE;
270 /* external storage */
271 ret = storage_ext_get_root(storage_id, root, sizeof(root), &extendedint);
273 _E("Failed to get type of external storage");
274 return STORAGE_ERROR_NOT_SUPPORTED;
277 *type = STORAGE_TYPE_EXTENDED_INTERNAL;
279 *type = STORAGE_TYPE_EXTERNAL;
281 return STORAGE_ERROR_NONE;
284 API int storage_get_state(int storage_id, storage_state_e *state)
286 const struct storage_ops *ops;
292 return STORAGE_ERROR_NOT_SUPPORTED;
295 _E("Invalid parameger");
296 return STORAGE_ERROR_INVALID_PARAMETER;
299 /* internal storage */
300 DD_LIST_FOREACH(st_int_head, elem, ops) {
301 if (ops->storage_id != storage_id)
303 *state = ops->get_state();
304 return STORAGE_ERROR_NONE;
307 /* external storage */
308 ret = storage_ext_get_state(storage_id, &st);
310 _E("Failed to get state (storage id(%d), ret(%d))", storage_id, ret); //LCOV_EXCL_LINE
311 return STORAGE_ERROR_OPERATION_FAILED;
315 return STORAGE_ERROR_NONE;
318 //LCOV_EXCL_START Not called Callback
319 static void compat_cb(int storage_id,
320 storage_dev_e dev, storage_state_e state,
321 const char *fstype, const char *fsuuid, const char *mountpath,
322 bool primary, int flags, void *user_data)
324 struct compat_cb_info* ccb_info;
327 if (storage_id == STORAGE_TYPE_EXTERNAL && dev == STORAGE_DEV_EXT_SDCARD)
328 DD_LIST_FOREACH(compat_cb_list, elem, ccb_info)
329 ccb_info->user_cb(storage_id, state, ccb_info->user_data);
333 API int storage_set_state_changed_cb(int storage_id, storage_state_changed_cb callback, void *user_data)
335 const struct storage_ops *st;
336 struct storage_cb_info info;
340 struct compat_cb_info* ccb_info;
341 static int compat_cb_init = 0;
344 return STORAGE_ERROR_NOT_SUPPORTED;
347 _E("Invalid parameger");
348 return STORAGE_ERROR_INVALID_PARAMETER;
351 /* For backward compatability */
352 if (storage_id == STORAGE_TYPE_EXTERNAL) {
353 if (!compat_cb_init) {
354 ret = storage_set_changed_cb(STORAGE_TYPE_EXTERNAL, compat_cb, NULL);
355 if (ret == STORAGE_ERROR_NONE)
361 ccb_info = malloc(sizeof(struct compat_cb_info));
362 if (ccb_info == NULL)
363 return STORAGE_ERROR_OPERATION_FAILED;
364 ccb_info->user_cb = callback;
365 ccb_info->user_data = user_data;
366 DD_LIST_APPEND(compat_cb_list, ccb_info);
368 return STORAGE_ERROR_NONE;
371 /* Internal storage does not support registering changed callback */
372 DD_LIST_FOREACH(st_int_head, elem, st)
373 if (st->storage_id == storage_id)
374 return STORAGE_ERROR_NONE;
376 /* external storage */
377 info.id = storage_id;
378 info.state_cb = callback;
379 info.user_data = user_data;
381 ret = storage_ext_register_cb(STORAGE_CALLBACK_ID, &info);
383 _E("Failed to register callback : id(%d)", storage_id); //LCOV_EXCL_LINE
384 return STORAGE_ERROR_OPERATION_FAILED;
387 return STORAGE_ERROR_NONE;
390 API int storage_unset_state_changed_cb(int storage_id, storage_state_changed_cb callback)
392 const struct storage_ops *st;
393 struct storage_cb_info info;
398 return STORAGE_ERROR_NOT_SUPPORTED;
401 _E("Invalid parameger");
402 return STORAGE_ERROR_INVALID_PARAMETER;
405 /* For backward compatability */
406 if (storage_id == STORAGE_TYPE_EXTERNAL) {
408 struct compat_cb_info* ccb_info;
410 DD_LIST_FOREACH_SAFE(compat_cb_list, elem, elem_n, ccb_info) {
411 if (ccb_info->user_cb == callback) {
412 DD_LIST_REMOVE(compat_cb_list, ccb_info);
414 return STORAGE_ERROR_NONE;
417 return STORAGE_ERROR_OPERATION_FAILED;
420 /* Internal storage does not support registering changed callback */
421 DD_LIST_FOREACH(st_int_head, elem, st)
422 if (st->storage_id == storage_id)
423 return STORAGE_ERROR_NONE;
425 /* external storage */
426 info.id = storage_id;
427 info.state_cb = callback;
429 ret = storage_ext_unregister_cb(STORAGE_CALLBACK_ID, &info);
431 _E("Failed to unregister callback : id(%d)", storage_id); //LCOV_EXCL_LINE
432 return STORAGE_ERROR_OPERATION_FAILED;
435 return STORAGE_ERROR_NONE;
438 API int storage_get_total_space(int storage_id, unsigned long long *bytes)
440 const struct storage_ops *st;
441 unsigned long long total;
446 return STORAGE_ERROR_NOT_SUPPORTED;
449 _E("Invalid parameger");
450 return STORAGE_ERROR_INVALID_PARAMETER;
453 /* internal storage */
454 DD_LIST_FOREACH(st_int_head, elem, st) {
455 if (st->storage_id != storage_id)
457 ret = st->get_space(&total, NULL);
461 /* external storage */
462 ret = storage_ext_get_space(storage_id, &total, NULL);
466 _E("Failed to get total memory : id(%d)", storage_id); //LCOV_EXCL_LINE
468 return STORAGE_ERROR_NOT_SUPPORTED;
469 return STORAGE_ERROR_OPERATION_FAILED;
473 return STORAGE_ERROR_NONE;
476 API int storage_get_available_space(int storage_id, unsigned long long *bytes)
478 const struct storage_ops *st;
479 unsigned long long avail;
484 return STORAGE_ERROR_NOT_SUPPORTED;
487 _E("Invalid parameger");
488 return STORAGE_ERROR_INVALID_PARAMETER;
491 /* internal storage */
492 DD_LIST_FOREACH(st_int_head, elem, st) {
493 if (st->storage_id != storage_id)
495 ret = st->get_space(NULL, &avail);
499 /* external storage */
500 ret = storage_ext_get_space(storage_id, NULL, &avail);
504 _E("Failed to get available memory : id(%d)", storage_id); //LCOV_EXCL_LINE
506 return STORAGE_ERROR_NOT_SUPPORTED;
507 return STORAGE_ERROR_OPERATION_FAILED;
511 return STORAGE_ERROR_NONE;
514 API int storage_set_changed_cb(storage_type_e type, storage_changed_cb callback, void *user_data)
517 struct storage_cb_info info;
519 if (type == STORAGE_TYPE_INTERNAL) {
520 _E("Internal storage is not supported");
521 return STORAGE_ERROR_NOT_SUPPORTED;
524 if (type != STORAGE_TYPE_EXTERNAL && type != STORAGE_TYPE_EXTENDED_INTERNAL) {
525 _E("Invalid type (%d)", type);
526 return STORAGE_ERROR_INVALID_PARAMETER;
530 _E("Callback is NULL");
531 return STORAGE_ERROR_INVALID_PARAMETER;
534 if (!storage_ext_is_supported()) {
535 _E("Block module is not enabled");
536 return STORAGE_ERROR_NOT_SUPPORTED;
539 /* external storage */
541 info.type_cb = callback;
542 info.user_data = user_data;
544 ret = storage_ext_register_cb(STORAGE_CALLBACK_TYPE, &info);
546 _E("Failed to register storage callback(ret:%d)", ret); //LCOV_EXCL_LINE
547 return STORAGE_ERROR_OPERATION_FAILED;
550 return STORAGE_ERROR_NONE;
553 API int storage_unset_changed_cb(storage_type_e type, storage_changed_cb callback)
555 struct storage_cb_info info;
558 if (type == STORAGE_TYPE_INTERNAL) {
559 _E("Internal storage is not supported");
560 return STORAGE_ERROR_NOT_SUPPORTED;
563 if (type != STORAGE_TYPE_EXTERNAL && type != STORAGE_TYPE_EXTENDED_INTERNAL) {
564 _E("Invalid type (%d)", type);
565 return STORAGE_ERROR_INVALID_PARAMETER;
569 _E("Callback is NULL");
570 return STORAGE_ERROR_INVALID_PARAMETER;
573 if (!storage_ext_is_supported()) {
574 _E("Block module is not enabled");
575 return STORAGE_ERROR_NOT_SUPPORTED;
578 /* external storage */
580 info.type_cb = callback;
582 ret = storage_ext_unregister_cb(STORAGE_CALLBACK_TYPE, &info);
584 _E("Failed to unregister storage callback(ret:%d)", ret); //LCOV_EXCL_LINE
585 return STORAGE_ERROR_OPERATION_FAILED;
588 return STORAGE_ERROR_NONE;
591 static void __CONSTRUCTOR__ init(void)
597 for (i = 0 ; i <= STORAGE_DIRECTORY_OTHERS ; i++) {
598 tmp = tzplatform_getenv(tz_id[i]);
600 token = rindex(tmp, '/');
603 dir_path[i] = strdup(token);