Only user session api calls can get internal storage root path
[platform/core/system/libstorage.git] / src / storage.c
index 598a289..d79a00d 100644 (file)
@@ -27,6 +27,9 @@
 #include "log.h"
 #include "storage-external.h"
 
+#define BLOCK_CONF_FILE         "/etc/storaged/block.conf"
+#define USER_UID_START         5000
+
 const char *dir_path[STORAGE_DIRECTORY_MAX];
 
 const int tz_id[STORAGE_DIRECTORY_MAX] = {
@@ -58,23 +61,49 @@ void remove_device(const struct storage_ops *st)
        DD_LIST_REMOVE(st_int_head, st);
 }
 
+int storage_ext_is_supported(void)
+{
+       static int support = -1;
+
+       if (support >= 0)
+               return support;
+
+       if (access(BLOCK_CONF_FILE, R_OK) == 0)
+               support = 1;
+       else
+               support = 0;
+
+       return support;
+}
+
 API int storage_foreach_device_supported(storage_device_supported_cb callback, void *user_data)
 {
        const struct storage_ops *st;
        dd_list *elem;
        int ret;
+       bool user = true;
 
        if (!callback) {
                _E("Invalid parameter");
                return STORAGE_ERROR_INVALID_PARAMETER;
        }
 
+       if (getuid() < USER_UID_START)
+               user = false;
+
        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;
+               if (user) {
+                       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;
+               }
+       }
+
+       if (!storage_ext_is_supported()) {
+               _D("Block module is not enabled");
+               return STORAGE_ERROR_NONE;
        }
 
        ret = storage_ext_foreach_device_list(callback, user_data);
@@ -92,6 +121,8 @@ API int storage_get_root_directory(int storage_id, char **path)
        dd_list *elem;
        char root[PATH_MAX];
        int ret;
+       bool extendedint;
+       bool user = true;
 
        if (storage_id < 0)
                return STORAGE_ERROR_NOT_SUPPORTED;
@@ -101,10 +132,19 @@ API int storage_get_root_directory(int storage_id, char **path)
                return STORAGE_ERROR_INVALID_PARAMETER;
        }
 
+       if (getuid() < USER_UID_START)
+               user = false;
+
        /* internal storage */
        DD_LIST_FOREACH(st_int_head, elem, st) {
                if (st->storage_id != storage_id)
                        continue;
+               if (!user) {
+                       _E("Only Tizen applications and user session daemons can use \
+                               storage_get_root_directory(id , ...)");
+                       return STORAGE_ERROR_INVALID_PARAMETER;
+               }
+
                *path = strdup(st->root());
                if (!*path) {
 //LCOV_EXCL_START System Error
@@ -116,7 +156,7 @@ API int storage_get_root_directory(int storage_id, char **path)
        }
 
        /* external storage */
-       ret = storage_ext_get_root(storage_id, root, sizeof(root));
+       ret = storage_ext_get_root(storage_id, root, sizeof(root), &extendedint);
        if (ret < 0) {
                _E("Failed to get root path of external storage(%d, %d", storage_id, ret); //LCOV_EXCL_LINE
                return STORAGE_ERROR_INVALID_PARAMETER;
@@ -140,6 +180,8 @@ API int storage_get_directory(int storage_id, storage_directory_e type, char **p
        int ret;
        dd_list *elem;
        bool found;
+       bool extendedint;
+       bool user = true;
 
        if (storage_id < 0)
                return STORAGE_ERROR_NOT_SUPPORTED;
@@ -163,7 +205,16 @@ API int storage_get_directory(int storage_id, storage_directory_e type, char **p
                break;
        }
 
+       if (getuid() < USER_UID_START)
+               user = false;
+
        if (found && st) {
+               if (!user) {
+                       _E("Only Tizen applications and user session daemons can use \
+                               storage_get_directory(id, ...)");
+                       return STORAGE_ERROR_INVALID_PARAMETER;
+               }
+
                snprintf(root, sizeof(root), "%s", st->root());
                if (type == STORAGE_DIRECTORY_SYSTEM_RINGTONES) {
                        temp2 = vconf_get_str(VCONFKEY_SETAPPL_CALL_RINGTONE_PATH_STR);
@@ -186,11 +237,14 @@ API int storage_get_directory(int storage_id, storage_directory_e type, char **p
                return STORAGE_ERROR_NOT_SUPPORTED;
        }
 
-       ret = storage_ext_get_root(storage_id, root, sizeof(root));
+       ret = storage_ext_get_root(storage_id, root, sizeof(root), &extendedint);
        if (ret < 0) {
                _E("Failed to get root dir for external storage(id:%d, ret:%d)", storage_id, ret); //LCOV_EXCL_LINE
-               return STORAGE_ERROR_OPERATION_FAILED;
+               return STORAGE_ERROR_NOT_SUPPORTED;
        }
+       /* The operation is not decided */
+       if (extendedint)
+               return STORAGE_ERROR_NOT_SUPPORTED;
 
        snprintf(temp, sizeof(temp), "%s/%s", root, dir_path[type]);
 
@@ -208,6 +262,9 @@ API int storage_get_type(int storage_id, storage_type_e *type)
 {
        const struct storage_ops *st;
        dd_list *elem;
+       char root[PATH_MAX];
+       int ret;
+       bool extendedint;
 
        if (storage_id < 0)
                return STORAGE_ERROR_NOT_SUPPORTED;
@@ -226,7 +283,15 @@ API int storage_get_type(int storage_id, storage_type_e *type)
        }
 
        /* external storage */
-       *type = STORAGE_TYPE_EXTERNAL;
+       ret = storage_ext_get_root(storage_id, root, sizeof(root), &extendedint);
+       if (ret < 0) {
+               _E("Failed to get type of external storage");
+               return STORAGE_ERROR_NOT_SUPPORTED;
+       }
+       if (extendedint)
+               *type = STORAGE_TYPE_EXTENDED_INTERNAL;
+       else
+               *type = STORAGE_TYPE_EXTERNAL;
 
        return STORAGE_ERROR_NONE;
 }
@@ -471,7 +536,7 @@ API int storage_set_changed_cb(storage_type_e type, storage_changed_cb callback,
                return STORAGE_ERROR_NOT_SUPPORTED;
        }
 
-       if (type != STORAGE_TYPE_EXTERNAL) {
+       if (type != STORAGE_TYPE_EXTERNAL && type != STORAGE_TYPE_EXTENDED_INTERNAL) {
                _E("Invalid type (%d)", type);
                return STORAGE_ERROR_INVALID_PARAMETER;
        }
@@ -481,6 +546,11 @@ API int storage_set_changed_cb(storage_type_e type, storage_changed_cb callback,
                return STORAGE_ERROR_INVALID_PARAMETER;
        }
 
+       if (!storage_ext_is_supported()) {
+               _E("Block module is not enabled");
+               return STORAGE_ERROR_NOT_SUPPORTED;
+       }
+
        /* external storage */
        info.type = type;
        info.type_cb = callback;
@@ -505,7 +575,7 @@ API int storage_unset_changed_cb(storage_type_e type, storage_changed_cb callbac
                return STORAGE_ERROR_NOT_SUPPORTED;
        }
 
-       if (type != STORAGE_TYPE_EXTERNAL) {
+       if (type != STORAGE_TYPE_EXTERNAL && type != STORAGE_TYPE_EXTENDED_INTERNAL) {
                _E("Invalid type (%d)", type);
                return STORAGE_ERROR_INVALID_PARAMETER;
        }
@@ -515,6 +585,11 @@ API int storage_unset_changed_cb(storage_type_e type, storage_changed_cb callbac
                return STORAGE_ERROR_INVALID_PARAMETER;
        }
 
+       if (!storage_ext_is_supported()) {
+               _E("Block module is not enabled");
+               return STORAGE_ERROR_NOT_SUPPORTED;
+       }
+
        /* external storage */
        info.type = type;
        info.type_cb = callback;