summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorGuorui Li <[email protected]>2025-07-31 17:36:28 +0800
committerGitHub <[email protected]>2025-07-31 17:36:28 +0800
commitd82dd71aefd6a4ec4d3e43698a396dd235bfdb0f (patch)
tree882695219744790a54370a75511ffe8b4eb25c01
parentc55cbc5cf29ef29913e46a909bade7e98e8b939f (diff)
[bugfix][component/dfs] fix bugs for function _get_parent_path(). (#10539)
* [bugfix][component/dfs]1.Skip the trailing slash character failed. 2. Scenario that parent path is root is not considered. * replace strdup() by rt_strdup(). * free memory after strdup(). * fix issue of not appending '\0' at end when parent path is root.
-rw-r--r--components/dfs/dfs_v2/src/dfs_file.c35
1 files changed, 25 insertions, 10 deletions
diff --git a/components/dfs/dfs_v2/src/dfs_file.c b/components/dfs/dfs_v2/src/dfs_file.c
index 57ff2e992f..d11758a7f0 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,24 +50,39 @@ static int _get_parent_path(const char *fullpath, char *path)
int len = 0;
char *str = 0;
- str = strrchr(fullpath, '/');
+ char *full_path = rt_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. */
+ {
+ path[0] = '/';
+ path[1] = '\0';
+ len = 1;
+ }
}
+ rt_free(full_path);
return len;
}
@@ -260,7 +275,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 +2371,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 +2398,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 +2426,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