Device info related functions cleanup 38/241238/9
authorMinje Ahn <minje.ahn@samsung.com>
Wed, 19 Aug 2020 00:39:05 +0000 (09:39 +0900)
committerMinje Ahn <minje.ahn@samsung.com>
Wed, 19 Aug 2020 23:55:59 +0000 (08:55 +0900)
Change-Id: I9b72d3ea1231b1fde09ce05c1badb69df899602a
Signed-off-by: Minje Ahn <minje.ahn@samsung.com>
src/common/media-common-external-storage.c

index a881ee8..475d79d 100755 (executable)
 int ms_read_device_info(const char *root_path, char **device_uuid)
 {
        int ret = MS_MEDIA_ERR_NONE;
-       int len = 0;
        GDir *dir = NULL;
        GError *error = NULL;
        const char *name;
-       bool find_item = false;
 
-       if (device_uuid == NULL) {
-               MS_DBG_ERR("device_uuid is NULL");
-               return MS_MEDIA_ERR_INVALID_PARAMETER;
-       }
+       MS_DBG_RETVM_IF(!device_uuid, MS_MEDIA_ERR_INVALID_PARAMETER, "device_uuid is NULL");
+       MS_DBG_RETVM_IF(!g_file_test(root_path, G_FILE_TEST_IS_DIR), MS_MEDIA_ERR_INVALID_PARAMETER, "Mount path does not exist");
 
-       if (!g_file_test(root_path, G_FILE_TEST_IS_DIR)) {
-               MS_DBG_ERR("Mount path is not exist");
-               *device_uuid = NULL;
-               return MS_MEDIA_ERR_INVALID_PARAMETER;
-       }
+       *device_uuid = NULL;
 
        dir = g_dir_open(root_path, 0, &error);
-       if (error == NULL && dir != NULL) {
-               *device_uuid = NULL;
-               while ((name = g_dir_read_name(dir))) {
-                       if (strncmp(name, DEVICE_INFO_FILE, strlen(DEVICE_INFO_FILE)) == 0) {
-                               MS_DBG_WARN("FIND DEV INFO : %s", name);
-                               len = strlen(name + strlen(DEVICE_INFO_FILE));
-                               if (len > 0) {
-                                       *device_uuid = g_strndup(name + strlen(DEVICE_INFO_FILE), MS_UUID_SIZE -1);
-                                       MS_DBG_WARN("[%s][DEV ID: %s]", root_path, *device_uuid);
-                               }
-
-                               if (*device_uuid == NULL)
-                                       ret = MS_MEDIA_ERR_INTERNAL;
-
-                               find_item = true;
-
-                               break;
-                       }
-               }
-       } else {
+       if  (error) {
                MS_DBG_ERR("g_dir_open failed [%s]", root_path);
-               if (error) {
-                       g_error_free(error);
-                       error = NULL;
-               }
-               *device_uuid = NULL;
+               g_error_free(error);
                return MS_MEDIA_ERR_INVALID_PARAMETER;
        }
 
+       while ((name = g_dir_read_name(dir))) {
+               if (g_str_has_prefix(name, DEVICE_INFO_FILE) && g_uuid_string_is_valid(name + strlen(DEVICE_INFO_FILE))) {
+                       *device_uuid = g_strndup(name + strlen(DEVICE_INFO_FILE), MS_UUID_SIZE - 1);
+                       MS_DBG_WARN("PATH[%s] DEV FILE[%s] DEV ID[%s]", root_path, name, *device_uuid);
+                       break;
+               }
+       }
+
        if (dir) {
                g_dir_close(dir);
                dir = NULL;
        }
 
-       if (!find_item) {
-               *device_uuid = NULL;
-               ret = MS_MEDIA_ERR_FILE_NOT_EXIST;
-               MS_DBG_ERR("[%s]DEV INFO NOT EXIST", root_path);
-       }
+       MS_DBG_RETVM_IF(*device_uuid == NULL, MS_MEDIA_ERR_FILE_NOT_EXIST, "Cannot found device info");
 
        return ret;
 }
 
 int ms_write_device_info(const char *root_path, char *device_uuid)
 {
-       FILE * fp = NULL;
-       int len = 0;
-       char path[MS_FILE_PATH_LEN_MAX] = {0,};
-
-       len = snprintf(path, MS_FILE_PATH_LEN_MAX -1, "%s/%s%s", root_path, DEVICE_INFO_FILE, device_uuid);
-       if (len < 0)
-               return MS_MEDIA_ERR_INVALID_PARAMETER;
+       int ret = MS_MEDIA_ERR_NONE;
+       char *path = NULL;
+       GError *err = NULL;
 
-       path[len] = '\0';
+       path = g_strdup_printf("%s/%s%s", root_path, DEVICE_INFO_FILE, device_uuid);
 
        if (g_file_test(path, G_FILE_TEST_IS_SYMLINK)) {
                MS_DBG_ERR("path is symbolic link. file name : %s", path);
+               g_free(path);
                return MS_MEDIA_ERR_INVALID_PARAMETER;
        }
 
-       fp = fopen(path, "wt");
-       if (fp == NULL) {
-               MS_DBG_ERR("fp is NULL. file name : %s", path);
-               if (errno == EROFS) {
-                       MS_DBG_STRERROR("read-only mode");
-                       return MS_MEDIA_ERR_NONE;
-               } else {
-                       return MS_MEDIA_ERR_INTERNAL;
-               }
+       if (!g_file_set_contents(path, NULL, 0, &err)) {
+               MS_DBG_ERR("g_file_set_contents failed[%s][%d]. path[%s]", err->message, err->code, path);
+               if (err->code != G_FILE_ERROR_ROFS)
+                       ret = MS_MEDIA_ERR_INTERNAL;
+
+               g_error_free(err);
        }
 
-       fclose(fp);
+       g_free(path);
 
-       return MS_MEDIA_ERR_NONE;
+       return ret;
 }
 
 int ms_get_added_storage_path(const char *add_path, char **device_id)
@@ -131,20 +98,20 @@ int ms_get_added_storage_path(const char *add_path, char **device_id)
 
        *device_id = NULL;
 
-       /* read deive information */
        ret = ms_read_device_info(add_path, &id);
-       if (id == NULL) {
-               if (ret == MS_MEDIA_ERR_FILE_NOT_EXIST) {
-                       ret = ms_genarate_uuid(&id);
-                       ret = ms_write_device_info(add_path , id);
-                       if (ret == MS_MEDIA_ERR_NONE)
-                               *device_id = g_strdup(id);
+       MS_DBG_RETV_IF(ret != MS_MEDIA_ERR_NONE && ret != MS_MEDIA_ERR_FILE_NOT_EXIST, ret);
+
+       if (ret == MS_MEDIA_ERR_FILE_NOT_EXIST) {
+               ret = ms_genarate_uuid(&id);
+               MS_DBG_RETV_IF(ret != MS_MEDIA_ERR_NONE, ret);
+               ret = ms_write_device_info(add_path, id);
+               if (ret != MS_MEDIA_ERR_NONE) {
+                       g_free(id);
+                       return ret;
                }
-       } else {
-               *device_id = g_strdup(id);
        }
 
-       g_free(id);
+       *device_id = id;
 
        return ret;
 }