internal: storage list is used just for internal storage 07/67707/1
authorTaeyoung Kim <ty317.kim@samsung.com>
Thu, 28 Apr 2016 06:10:16 +0000 (15:10 +0900)
committerTaeyoung Kim <ty317.kim@samsung.com>
Thu, 28 Apr 2016 06:15:22 +0000 (15:15 +0900)
- External storages are changeable and can be added/removed
  frequently. Thus storage list maintains just internal storage.

Change-Id: Ia4da6c41b29156fdd8f783cadc371acbf8699be2
Signed-off-by: Taeyoung Kim <ty317.kim@samsung.com>
include/common.h
src/storage-internal.c
src/storage.c

index 1a9d987..7ff5ce9 100644 (file)
@@ -59,6 +59,7 @@ struct storage_ops {
        int (*get_space) (unsigned long long *total, unsigned long long *available);
        int (*register_cb) (enum storage_cb_type type, struct storage_cb_info *info);
        int (*unregister_cb) (enum storage_cb_type type, struct storage_cb_info *info);
+       int storage_id;
 };
 
 #define STORAGE_OPS_REGISTER(st)       \
index ca8b182..1867def 100755 (executable)
@@ -68,6 +68,7 @@ const struct storage_ops internal = {
        .root = internal_get_root,
        .get_state = internal_get_state,
        .get_space = internal_get_space,
+       .storage_id = 0,
 };
 
 STORAGE_OPS_REGISTER(&internal)
index e21b0cd..cb4409a 100644 (file)
@@ -37,61 +37,65 @@ const char *dir_path[STORAGE_DIRECTORY_MAX] = {
        [STORAGE_DIRECTORY_SYSTEM_RINGTONES] = "",
 };
 
-static dd_list *st_head;
+static dd_list *st_int_head; /* Internal storage list */
 
 void add_device(const struct storage_ops *st)
 {
-       DD_LIST_APPEND(st_head, st);
+       DD_LIST_APPEND(st_int_head, st);
 }
 
 void remove_device(const struct storage_ops *st)
 {
-       DD_LIST_REMOVE(st_head, st);
+       DD_LIST_REMOVE(st_int_head, st);
 }
 
 API int storage_foreach_device_supported(storage_device_supported_cb callback, void *user_data)
 {
        const struct storage_ops *st;
        dd_list *elem;
-       int storage_id = 0, ret;
+       int ret;
 
        if (!callback) {
                _E("Invalid parameter");
                return STORAGE_ERROR_INVALID_PARAMETER;
        }
 
-       DD_LIST_FOREACH(st_head, elem, st) {
-               ret = callback(storage_id, st->type, st->get_state(),
+       DD_LIST_FOREACH(st_int_head, elem, st) {
+               ret = callback(st->storage_id, st->type, st->get_state(),
                                st->root(), user_data);
                /* if the return value is false, will be stop to iterate */
                if (!ret)
                        break;
-               storage_id++;
        }
 
+       /* TODO external storage */
+
        return STORAGE_ERROR_NONE;
 }
 
 API int storage_get_root_directory(int storage_id, char **path)
 {
        const struct storage_ops *st;
+       dd_list *elem;
 
-       if (!path) {
+       if (!path || storage_id < 0) {
                _E("Invalid parameger");
                return STORAGE_ERROR_INVALID_PARAMETER;
        }
 
-       st = DD_LIST_NTH(st_head, storage_id);
-       if (!st) {
-               _E("Not supported storage : id(%d)", storage_id);
-               return STORAGE_ERROR_NOT_SUPPORTED;
+       /* internal storage */
+       DD_LIST_FOREACH(st_int_head, elem, st) {
+               if (st->storage_id != storage_id)
+                       continue;
+               *path = strdup(st->root());
+               if (!*path) {
+                       _E("Failed to copy the root string : %d", errno);
+                       return STORAGE_ERROR_OUT_OF_MEMORY;
+               }
+               return STORAGE_ERROR_NONE;
        }
 
-       *path = strdup(st->root());
-       if (!*path) {
-               _E("Failed to copy the root string : %d", errno);
-               return STORAGE_ERROR_OUT_OF_MEMORY;
-       }
+       /* TODO external storage */
 
        return STORAGE_ERROR_NONE;
 }
@@ -99,12 +103,14 @@ API int storage_get_root_directory(int storage_id, char **path)
 API int storage_get_directory(int storage_id, storage_directory_e type, char **path)
 {
        const struct storage_ops *st;
-       const char *root;
+       char root[PATH_MAX];
        char temp[PATH_MAX];
        char *temp2, *end;
        int ret;
+       dd_list *elem;
+       bool found;
 
-       if (!path) {
+       if (!path || storage_id < 0) {
                _E("Invalid parameger");
                return STORAGE_ERROR_INVALID_PARAMETER;
        }
@@ -114,33 +120,38 @@ API int storage_get_directory(int storage_id, storage_directory_e type, char **p
                return STORAGE_ERROR_INVALID_PARAMETER;
        }
 
-       st = DD_LIST_NTH(st_head, storage_id);
-       if (!st) {
-               _E("Not supported storage : id(%d)", storage_id);
-               return STORAGE_ERROR_NOT_SUPPORTED;
+       /* internal storage */
+       found = false;
+       DD_LIST_FOREACH(st_int_head, elem, st) {
+               if (st->storage_id != storage_id)
+                       continue;
+               found = true;
+               break;
        }
 
-       if (st->type != STORAGE_TYPE_INTERNAL
-           && type == STORAGE_DIRECTORY_SYSTEM_RINGTONES) {
-               _E("Not support directory : id(%d) type(%d)", storage_id, type);
-               return STORAGE_ERROR_NOT_SUPPORTED;
+       if (found && st) {
+               snprintf(root, sizeof(root), "%s", st->root());
+               if (type == STORAGE_DIRECTORY_SYSTEM_RINGTONES) {
+                       ret = system_settings_get_value_string(SYSTEM_SETTINGS_KEY_INCOMING_CALL_RINGTONE, &temp2);
+                       if (ret < 0) {
+                               _E("Failed to get ringtone path : %d", ret);
+                               return STORAGE_ERROR_OPERATION_FAILED;
+                       }
+                       end = strrchr(temp2, '/');
+                       if (end)
+                               *end = '\0';
+                       snprintf(temp, PATH_MAX, "%s", temp2);
+                       free(temp2);
+               } else
+                       snprintf(temp, PATH_MAX, "%s/%s", root, dir_path[type]);
+
+               goto out;
        }
 
-       root = st->root();
-       if (type == STORAGE_DIRECTORY_SYSTEM_RINGTONES) {
-               ret = system_settings_get_value_string(SYSTEM_SETTINGS_KEY_INCOMING_CALL_RINGTONE, &temp2);
-               if (ret < 0) {
-                       _E("Failed to get ringtone path : %d", ret);
-                       return STORAGE_ERROR_OPERATION_FAILED;
-               }
-               end = strrchr(temp2, '/');
-               if (end)
-                       *end = '\0';
-               snprintf(temp, PATH_MAX, "%s", temp2);
-               free(temp2);
-       } else
-               snprintf(temp, PATH_MAX, "%s/%s", root, dir_path[type]);
+       /* external storage */
+       return STORAGE_ERROR_NONE;
 
+out:
        *path = strdup(temp);
        if (!*path) {
                _E("Failed to copy the directory(%d) string : %d", type, errno);
@@ -153,39 +164,45 @@ API int storage_get_directory(int storage_id, storage_directory_e type, char **p
 API int storage_get_type(int storage_id, storage_type_e *type)
 {
        const struct storage_ops *st;
+       dd_list *elem;
 
-       if (!type) {
+       if (!type || storage_id < 0) {
                _E("Invalid parameger");
                return STORAGE_ERROR_INVALID_PARAMETER;
        }
 
-       st = DD_LIST_NTH(st_head, storage_id);
-       if (!st) {
-               _E("Not supported storage : id(%d)", storage_id);
-               return STORAGE_ERROR_NOT_SUPPORTED;
+       /* internal storage */
+       DD_LIST_FOREACH(st_int_head, elem, st) {
+               if (st->storage_id != storage_id)
+                       continue;
+               *type = st->type;
+               return STORAGE_ERROR_NONE;
        }
 
-       *type = st->type;
+       /* external storage */
 
        return STORAGE_ERROR_NONE;
 }
 
 API int storage_get_state(int storage_id, storage_state_e *state)
 {
-       const struct storage_ops *st;
+       const struct storage_ops *ops;
+       dd_list *elem;
 
        if (!state) {
                _E("Invalid parameger");
                return STORAGE_ERROR_INVALID_PARAMETER;
        }
 
-       st = DD_LIST_NTH(st_head, storage_id);
-       if (!st) {
-               _E("Not supported storage : id(%d)", storage_id);
-               return STORAGE_ERROR_NOT_SUPPORTED;
+       /* internal storage */
+       DD_LIST_FOREACH(st_int_head, elem, ops) {
+               if (ops->storage_id != storage_id)
+                       continue;
+               *state = ops->get_state();
+               return STORAGE_ERROR_NONE;
        }
 
-       *state = st->get_state();
+       /* external storage */
 
        return STORAGE_ERROR_NONE;
 }
@@ -193,33 +210,19 @@ API int storage_get_state(int storage_id, storage_state_e *state)
 API int storage_set_state_changed_cb(int storage_id, storage_state_changed_cb callback, void *user_data)
 {
        const struct storage_ops *st;
-       struct storage_cb_info info;
-       int ret;
+       dd_list *elem;
 
        if (!callback) {
                _E("Invalid parameger");
                return STORAGE_ERROR_INVALID_PARAMETER;
        }
 
-       st = DD_LIST_NTH(st_head, storage_id);
-       if (!st) {
-               _E("Not supported storage : id(%d)", storage_id);
-               return STORAGE_ERROR_NOT_SUPPORTED;
-       }
-
-       /* do not register changed callback in case of internal memory */
-       if (st->type == STORAGE_TYPE_INTERNAL)
-               return STORAGE_ERROR_NONE;
-
-       info.id = storage_id;
-       info.state_cb = callback;
-       info.user_data = user_data;
+       /* Internal storage does not support registering changed callback */
+       DD_LIST_FOREACH(st_int_head, elem, st)
+               if (st->storage_id == storage_id)
+                       return STORAGE_ERROR_NONE;
 
-       ret = st->register_cb(STORAGE_CALLBACK_STATE, &info);
-       if (ret < 0) {
-               _E("Failed to register callback : id(%d)", storage_id);
-               return STORAGE_ERROR_OPERATION_FAILED;
-       }
+       /* external storage */
 
        return STORAGE_ERROR_NONE;
 }
@@ -227,32 +230,17 @@ API int storage_set_state_changed_cb(int storage_id, storage_state_changed_cb ca
 API int storage_unset_state_changed_cb(int storage_id, storage_state_changed_cb callback)
 {
        const struct storage_ops *st;
-       struct storage_cb_info info;
-       int ret;
+       dd_list *elem;
 
        if (!callback) {
                _E("Invalid parameger");
                return STORAGE_ERROR_INVALID_PARAMETER;
        }
 
-       st = DD_LIST_NTH(st_head, storage_id);
-       if (!st) {
-               _E("Not supported storage : id(%d)", storage_id);
-               return STORAGE_ERROR_NOT_SUPPORTED;
-       }
-
-       /* in case of internal memory, it does not register changed callback */
-       if (st->type == STORAGE_TYPE_INTERNAL)
-               return STORAGE_ERROR_NONE;
-
-       info.id = storage_id;
-       info.state_cb = callback;
-
-       ret = st->unregister_cb(STORAGE_CALLBACK_STATE, &info);
-       if (ret < 0) {
-               _E("Failed to unregister callback : id(%d)", storage_id);
-               return STORAGE_ERROR_OPERATION_FAILED;
-       }
+       /* Internal storage does not support registering changed callback */
+       DD_LIST_FOREACH(st_int_head, elem, st)
+               if (st->storage_id == storage_id)
+                       return STORAGE_ERROR_NONE;
 
        return STORAGE_ERROR_NONE;
 }
@@ -262,26 +250,31 @@ API int storage_get_total_space(int storage_id, unsigned long long *bytes)
        const struct storage_ops *st;
        unsigned long long total;
        int ret;
+       dd_list *elem;
 
-       if (!bytes) {
+       if (!bytes || storage_id < 0) {
                _E("Invalid parameger");
                return STORAGE_ERROR_INVALID_PARAMETER;
        }
 
-       st = DD_LIST_NTH(st_head, storage_id);
-       if (!st) {
-               _E("Not supported storage : id(%d)", storage_id);
-               return STORAGE_ERROR_NOT_SUPPORTED;
+       /* internal storage */
+       DD_LIST_FOREACH(st_int_head, elem, st) {
+               if (st->storage_id != storage_id)
+                       continue;
+               ret = st->get_space(&total, NULL);
+               goto out;
        }
 
-       ret = st->get_space(&total, NULL);
+       /* external storage */
+       ret = 0;
+
+out:
        if (ret < 0) {
                _E("Failed to get total memory : id(%d)", storage_id);
                return STORAGE_ERROR_OPERATION_FAILED;
        }
 
        *bytes = total;
-
        return STORAGE_ERROR_NONE;
 }
 
@@ -290,25 +283,30 @@ API int storage_get_available_space(int storage_id, unsigned long long *bytes)
        const struct storage_ops *st;
        unsigned long long avail;
        int ret;
+       dd_list *elem;
 
        if (!bytes) {
                _E("Invalid parameger");
                return STORAGE_ERROR_INVALID_PARAMETER;
        }
 
-       st = DD_LIST_NTH(st_head, storage_id);
-       if (!st) {
-               _E("Not supported storage : id(%d)", storage_id);
-               return STORAGE_ERROR_NOT_SUPPORTED;
+       /* internal storage */
+       DD_LIST_FOREACH(st_int_head, elem, st) {
+               if (st->storage_id != storage_id)
+                       continue;
+               ret = st->get_space(NULL, &avail);
+               goto out;
        }
 
-       ret = st->get_space(NULL, &avail);
+       /* external storage */
+       ret = 0;
+
+out:
        if (ret < 0) {
                _E("Failed to get available memory : id(%d)", storage_id);
                return STORAGE_ERROR_OPERATION_FAILED;
        }
 
        *bytes = avail;
-
        return STORAGE_ERROR_NONE;
 }