[bugfix][component/dfs] fix bugs for function _get_parent_path(). (#10539)
Some checks failed
ToolsCI / Tools (push) Has been cancelled
AutoTestCI / components/cpp11 (push) Has been cancelled
AutoTestCI / kernel/atomic (push) Has been cancelled
AutoTestCI / kernel/atomic/riscv64 (push) Has been cancelled
AutoTestCI / kernel/atomic_c11 (push) Has been cancelled
AutoTestCI / kernel/atomic_c11/riscv64 (push) Has been cancelled
AutoTestCI / kernel/device (push) Has been cancelled
AutoTestCI / kernel/ipc (push) Has been cancelled
AutoTestCI / kernel/irq (push) Has been cancelled
AutoTestCI / kernel/mem (push) Has been cancelled
AutoTestCI / kernel/mem/riscv64 (push) Has been cancelled
AutoTestCI / kernel/thread (push) Has been cancelled
AutoTestCI / kernel/timer (push) Has been cancelled
AutoTestCI / rtsmart/aarch64 (push) Has been cancelled
AutoTestCI / rtsmart/arm (push) Has been cancelled
AutoTestCI / rtsmart/riscv64 (push) Has been cancelled
AutoTestCI / components/utest (push) Has been cancelled
RT-Thread BSP Static Build Check / 🔍 Summary of Git Diff Changes (push) Has been cancelled
RT-Thread BSP Static Build Check / ${{ matrix.legs.RTT_BSP }} (push) Has been cancelled
RT-Thread BSP Static Build Check / collect-artifacts (push) Has been cancelled
doc_doxygen / doxygen_doc generate (push) Has been cancelled
doc_doxygen / deploy (push) Has been cancelled
pkgs_test / change (push) Has been cancelled
utest_auto_run / AARCH64-rtsmart :default.cfg (push) Has been cancelled
utest_auto_run / A9-rtsmart :default.cfg (push) Has been cancelled
utest_auto_run / RISCV-rtsmart :default.cfg (push) Has been cancelled
utest_auto_run / XUANTIE-rtsmart :default.cfg (push) Has been cancelled
utest_auto_run / AARCH64 :default.cfg (push) Has been cancelled
utest_auto_run / A9 :default.cfg (push) Has been cancelled
utest_auto_run / A9-smp :default.cfg (push) Has been cancelled
utest_auto_run / RISCV :default.cfg (push) Has been cancelled
manual_trigger_update_all / update and create pull request (push) Has been cancelled
manual_trigger_scons_build_all / ${{ github.event.inputs.bsp_options }} (push) Has been cancelled

* [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.
This commit is contained in:
Guorui Li
2025-07-31 17:36:28 +08:00
committed by GitHub
parent c55cbc5cf2
commit d82dd71aef

View File

@@ -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