From a53870681510fb87e53bac0e4c8c50f1f0b209f9 Mon Sep 17 00:00:00 2001 From: ligr Date: Fri, 25 Jul 2025 12:36:15 +0800 Subject: [PATCH 1/4] [bugfix][component/dfs]1.Skip the trailing slash character failed. 2. Scenario that parent path is root is not considered. --- components/dfs/dfs_v2/src/dfs_file.c | 33 +++++++++++++++++++--------- 1 file changed, 23 insertions(+), 10 deletions(-) diff --git a/components/dfs/dfs_v2/src/dfs_file.c b/components/dfs/dfs_v2/src/dfs_file.c index 57ff2e992ff..f66e88bce0a 100644 --- a/components/dfs/dfs_v2/src/dfs_file.c +++ b/components/dfs/dfs_v2/src/dfs_file.c @@ -1,5 +1,5 @@ /* - * Copyright (c) 2006-2023, RT-Thread Development Team + * Copyright (c) 2006-2025 RT-Thread Development Team * * SPDX-License-Identifier: Apache-2.0 * @@ -50,22 +50,35 @@ static int _get_parent_path(const char *fullpath, char *path) int len = 0; char *str = 0; - str = strrchr(fullpath, '/'); + char *full_path = strdup(fullpath); + if (full_path == NULL) + { + rt_set_errno(ENOMEM); + return -1; + } + + str = strrchr(full_path, '/'); /* skip last '/' */ if (str && *(str + 1) == '\0') { - str = strrchr(str - 1, '/'); + *str = '\0'; + str = strrchr(full_path, '/'); } if (str) { - len = str - fullpath; + len = str - full_path; if (len > 0) { - rt_memcpy(path, fullpath, len); + rt_memcpy(path, full_path, len); path[len] = '\0'; } + else if (len == 0) /* parent path is root path. */ + { + rt_memcpy(path, "/", 1); + len = 1; + } } return len; @@ -260,7 +273,7 @@ char *dfs_file_realpath(struct dfs_mnt **mnt, const char *fullpath, int mode) { int len, link_len; - path = (char *)rt_malloc((DFS_PATH_MAX * 3) + 3); // path + \0 + link_fn + \0 + tmp_path + \0 + path = (char *)rt_malloc((DFS_PATH_MAX * 3) + 3); /* path + \0 + link_fn + \0 + tmp_path + \0 */ if (!path) { return RT_NULL; @@ -2356,14 +2369,14 @@ void copy(const char *src, const char *dst) flag |= FLAG_DST_IS_FILE; } - //2. check status + /* 2. check status */ if ((flag & FLAG_SRC_IS_DIR) && (flag & FLAG_DST_IS_FILE)) { rt_kprintf("cp faild, cp dir to file is not permitted!\n"); return ; } - //3. do copy + /* 3. do copy */ if (flag & FLAG_SRC_IS_FILE) { if (flag & FLAG_DST_IS_DIR) @@ -2383,7 +2396,7 @@ void copy(const char *src, const char *dst) copyfile(src, dst); } } - else //flag & FLAG_SRC_IS_DIR + else /* flag & FLAG_SRC_IS_DIR */ { if (flag & FLAG_DST_IS_DIR) { @@ -2411,4 +2424,4 @@ void copy(const char *src, const char *dst) } FINSH_FUNCTION_EXPORT(copy, copy file or dir) -#endif +#endif \ No newline at end of file From 842d527396ffb8e04c468111950ae05d338cbfb5 Mon Sep 17 00:00:00 2001 From: ligr Date: Fri, 25 Jul 2025 12:58:22 +0800 Subject: [PATCH 2/4] replace strdup() by rt_strdup(). --- components/dfs/dfs_v2/src/dfs_file.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/components/dfs/dfs_v2/src/dfs_file.c b/components/dfs/dfs_v2/src/dfs_file.c index f66e88bce0a..fe36b8ffbbb 100644 --- a/components/dfs/dfs_v2/src/dfs_file.c +++ b/components/dfs/dfs_v2/src/dfs_file.c @@ -50,7 +50,7 @@ static int _get_parent_path(const char *fullpath, char *path) int len = 0; char *str = 0; - char *full_path = strdup(fullpath); + char *full_path = rt_strdup(fullpath); if (full_path == NULL) { rt_set_errno(ENOMEM); From 54e300be723e9cf749b7e0487b20667273172cb0 Mon Sep 17 00:00:00 2001 From: ligr Date: Fri, 25 Jul 2025 15:09:06 +0800 Subject: [PATCH 3/4] free memory after strdup(). --- components/dfs/dfs_v2/src/dfs_file.c | 1 + 1 file changed, 1 insertion(+) diff --git a/components/dfs/dfs_v2/src/dfs_file.c b/components/dfs/dfs_v2/src/dfs_file.c index fe36b8ffbbb..25cfdaca142 100644 --- a/components/dfs/dfs_v2/src/dfs_file.c +++ b/components/dfs/dfs_v2/src/dfs_file.c @@ -81,6 +81,7 @@ static int _get_parent_path(const char *fullpath, char *path) } } + rt_free(full_path); return len; } From 5ca3dc4a39403920eefd4655ad4a1a2acdb9c2e6 Mon Sep 17 00:00:00 2001 From: ligr Date: Tue, 29 Jul 2025 10:15:08 +0800 Subject: [PATCH 4/4] fix issue of not appending '\0' at end when parent path is root. --- components/dfs/dfs_v2/src/dfs_file.c | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/components/dfs/dfs_v2/src/dfs_file.c b/components/dfs/dfs_v2/src/dfs_file.c index 25cfdaca142..d11758a7f0f 100644 --- a/components/dfs/dfs_v2/src/dfs_file.c +++ b/components/dfs/dfs_v2/src/dfs_file.c @@ -76,7 +76,8 @@ static int _get_parent_path(const char *fullpath, char *path) } else if (len == 0) /* parent path is root path. */ { - rt_memcpy(path, "/", 1); + path[0] = '/'; + path[1] = '\0'; len = 1; } }