Improve path trim function 27/228427/3
authorMinje Ahn <minje.ahn@samsung.com>
Mon, 23 Mar 2020 05:26:18 +0000 (14:26 +0900)
committerMinje Ahn <minje.ahn@samsung.com>
Mon, 23 Mar 2020 06:32:16 +0000 (15:32 +0900)
Change-Id: I5e370efa45e91124082af6de8bdeb3f924222a39
Signed-off-by: Minje Ahn <minje.ahn@samsung.com>
src/media_util_private.c

index 672b28a6eec891705cdf7f26ebeee64992275bb2..e621f810e28d06921aa82c94cd90c616731b40b4 100755 (executable)
@@ -83,29 +83,30 @@ int _media_util_check_file_exist(const char *path)
 
 void _media_util_trim_path(const char *input_path, char **output_path)
 {
-       char buf[4096] = {0,};
-       char tmp[4096] = {0,};
-       char *pos = NULL;
-
-       memset(buf, 0, sizeof(buf));
-       if (!SAFE_STRLCPY(buf, input_path, sizeof(buf)))
-               media_content_sec_debug("Truncation occurred[%zu]", strlen(input_path));
-
-       while ((pos = strstr(buf, "//")) != NULL) {
-               memset(tmp, 0, sizeof(tmp));
-               if (!SAFE_STRLCPY(tmp, buf, pos - buf + 1))
-                       media_content_sec_debug("Truncation occurred");
-               SAFE_STRLCAT(tmp, pos + 1, sizeof(tmp));
-
-               memset(buf, 0, sizeof(buf));
-               if (!SAFE_STRLCPY(buf, tmp, sizeof(buf)))
-                       media_content_sec_debug("Truncation occurred[%zu]", strlen(tmp));
-       }
-
-       if (g_str_has_suffix(buf, "/"))
-               *output_path = g_strndup(buf, strlen(buf) - 1);
-       else
-               *output_path = g_strdup(buf);
+       gchar **name_list = NULL;
+       gchar *tmp_path = NULL;
+
+       if (!STRING_VALID(input_path) || output_path == NULL)
+               return;
+
+       /* Workflow example
+               Input : /a/b//c/
+               After g_strsplit() : {'','a','b','','c',''}
+               After g_build_pathv() : a/b/c
+               After g_strdup_printf() : /a/b/c
+       */
+       name_list = g_strsplit(input_path, "/", -1);
+       if (!name_list)
+               return;
+
+       tmp_path = g_build_pathv(G_DIR_SEPARATOR_S, name_list);
+       g_strfreev(name_list);
+       if (!tmp_path)
+               return;
+
+       /* g_build_pathv does not add root '/' */
+       *output_path = g_strdup_printf("/%s", tmp_path);
+       g_free(tmp_path);
 }