Fix error value in storage_get_primary_sdcard
[platform/core/system/libstorage.git] / src / storage-inhouse.c
index 7b89923..193de46 100755 (executable)
@@ -15,6 +15,8 @@
  */
 
 
+#include <unistd.h>
+#include <sys/types.h>
 #include <stdio.h>
 #include <stdlib.h>
 #include <string.h>
@@ -26,6 +28,7 @@
 #include "log.h"
 #include "storage-internal.h"
 #include "storage-external-dbus.h"
+#include "storage-experimental.h"
 
 /*
        Get compat path from origin Multi-user path
@@ -46,6 +49,7 @@
                else
                        // can convert. use dest path
  */
+//LCOV_EXCL_START Untested function
 API int storage_get_compat_internal_path(const char* origin, int len, char* compat)
 {
        int r = -1;
@@ -57,13 +61,23 @@ API int storage_get_compat_internal_path(const char* origin, int len, char* comp
                return -1;
        }
 
+       if (getuid() <= USER_UID_START) {
+               //LCOV_EXCL_START System Error
+               _E("Only apps and user session daemons are allowed "
+                               "to use storage_get_compat_internal_path()");
+               return -1;
+               //LCOV_EXCL_STOP
+       }
+
        // this API works on place where compat path is bind-mounted
        if (!is_compat_bind_mount()) {
+               //LCOV_EXCL_START System Error
                _E("No compat bind mount");
                return -1;
+               //LCOV_EXCL_STOP
        }
 
-       str = tzplatform_getenv(TZ_USER_CONTENT);
+       str = tzplatform_uid_getenv(getuid(), TZ_USER_CONTENT);
        str_len = strlen(str);
        if (strncmp(origin, str, str_len) != 0) {
                _E("Failed to match TZ_USER_CONTENT");
@@ -72,12 +86,15 @@ API int storage_get_compat_internal_path(const char* origin, int len, char* comp
 
        r = snprintf(compat, len, "%s%s", COMPAT_DIR, origin + str_len);
        if (r < 0) {
+               //LCOV_EXCL_START System Error
                _E("Failed to create new path");
                return -1;
+               //LCOV_EXCL_STOP
        }
 
        return 0;
 }
+//LCOV_EXCL_STOP
 
 /*
        Get Multi-user path from compat path
@@ -108,10 +125,20 @@ API int storage_get_origin_internal_path(const char* compat, int len, char* orig
                return -1;
        }
 
+       if (getuid() <= USER_UID_START) {
+               //LCOV_EXCL_START System Error
+               _E("Only apps and user session daemons are allowed "
+                               "to use storage_get_origin_internal_path()");
+               return -1;
+               //LCOV_EXCL_STOP
+       }
+
        // this API works on place where compat path is bind-mounted
        if (!is_compat_bind_mount()) {
+               //LCOV_EXCL_START System Error
                _E("no compat bind mount");
                return -1;
+               //LCOV_EXCL_STOP
        }
 
        compat_len = strlen(COMPAT_DIR);
@@ -120,10 +147,12 @@ API int storage_get_origin_internal_path(const char* compat, int len, char* orig
                return -1;
        }
 
-       r = snprintf(origin, len, "%s%s", tzplatform_getenv(TZ_USER_CONTENT), compat + compat_len);
+       r = snprintf(origin, len, "%s%s", tzplatform_uid_getenv(getuid(), TZ_USER_CONTENT), compat + compat_len);
        if (r < 0) {
+               //LCOV_EXCL_START System Error
                _E("failed to create new path");
                return -1;
+               //LCOV_EXCL_STOP
        }
 
        return 0;
@@ -137,14 +166,19 @@ API int storage_get_primary_sdcard(int *storage_id, char **path)
        if (!storage_id || !path)
                return STORAGE_ERROR_INVALID_PARAMETER;
 
+       if (!storage_ext_is_supported())
+               return STORAGE_ERROR_NOT_SUPPORTED;
+
        result = dbus_method_call_sync(STORAGE_EXT_BUS_NAME,
                        STORAGE_EXT_PATH_MANAGER,
                        STORAGE_EXT_IFACE_MANAGER,
                        "GetMmcPrimary",
                        NULL);
        if (!result) {
+               //LCOV_EXCL_START System Error
                _E("Failed to get primary sdcard partition"); //LCOV_EXCL_LINE
                return STORAGE_ERROR_OPERATION_FAILED; //LCOV_EXCL_LINE
+               //LCOV_EXCL_STOP
        }
 
        g_variant_get(result, "(issssssisibii)",
@@ -162,7 +196,7 @@ API int storage_get_primary_sdcard(int *storage_id, char **path)
 
        *path = strdup(info.mount_point);
        if (*path == NULL)
-               return STORAGE_ERROR_OUT_OF_MEMORY;
+               return STORAGE_ERROR_OUT_OF_MEMORY; //LCOV_EXCL_LINE System Error
 
        *storage_id = info.storage_id;
 
@@ -176,7 +210,7 @@ API int storage_get_type_dev(int storage_id, storage_type_e *type, storage_dev_e
 
        if (storage_id < 0) {
                _E("Invalid parameger");
-               return STORAGE_ERROR_NOT_SUPPORTED;
+               return STORAGE_ERROR_NO_DEVICE;
        }
 
        if (!type) {
@@ -194,19 +228,21 @@ API int storage_get_type_dev(int storage_id, storage_type_e *type, storage_dev_e
                _E("Failed to get storage type: %d", ret);
                return ret;
        }
+       if (*type == STORAGE_TYPE_INTERNAL)
+               return STORAGE_ERROR_NONE;
 
        ext_dev = calloc(1, sizeof(storage_ext_device));
        if (!ext_dev) {
-//LCOV_EXCL_START System Error
+               //LCOV_EXCL_START System Error
                _E("calloc failed");
                return STORAGE_ERROR_OUT_OF_MEMORY;
-//LCOV_EXCL_STOP
+               //LCOV_EXCL_STOP
        }
 
        ret = storage_ext_get_device_info(storage_id, ext_dev);
        if (ret < 0) {
                _E("Cannot get the storage with id (%d, ret:%d)", storage_id, ret); //LCOV_EXCL_LINE
-               ret = STORAGE_ERROR_NOT_SUPPORTED;
+               ret = STORAGE_ERROR_NO_DEVICE;
                goto out;
        }