11/*
2- * Copyright (c) 2006-2023, RT-Thread Development Team
2+ * Copyright (c) 2006-2025 RT-Thread Development Team
33 *
44 * SPDX-License-Identifier: Apache-2.0
55 *
@@ -50,22 +50,35 @@ static int _get_parent_path(const char *fullpath, char *path)
5050 int len = 0 ;
5151 char * str = 0 ;
5252
53- str = strrchr (fullpath , '/' );
53+ char * full_path = strdup (fullpath );
54+ if (full_path == NULL )
55+ {
56+ rt_set_errno (ENOMEM );
57+ return -1 ;
58+ }
59+
60+ str = strrchr (full_path , '/' );
5461
5562 /* skip last '/' */
5663 if (str && * (str + 1 ) == '\0' )
5764 {
58- str = strrchr (str - 1 , '/' );
65+ * str = '\0' ;
66+ str = strrchr (full_path , '/' );
5967 }
6068
6169 if (str )
6270 {
63- len = str - fullpath ;
71+ len = str - full_path ;
6472 if (len > 0 )
6573 {
66- rt_memcpy (path , fullpath , len );
74+ rt_memcpy (path , full_path , len );
6775 path [len ] = '\0' ;
6876 }
77+ else if (len == 0 ) /* parent path is root path. */
78+ {
79+ rt_memcpy (path , "/" , 1 );
80+ len = 1 ;
81+ }
6982 }
7083
7184 return len ;
@@ -260,7 +273,7 @@ char *dfs_file_realpath(struct dfs_mnt **mnt, const char *fullpath, int mode)
260273 {
261274 int len , link_len ;
262275
263- path = (char * )rt_malloc ((DFS_PATH_MAX * 3 ) + 3 ); // path + \0 + link_fn + \0 + tmp_path + \0
276+ path = (char * )rt_malloc ((DFS_PATH_MAX * 3 ) + 3 ); /* path + \0 + link_fn + \0 + tmp_path + \0 */
264277 if (!path )
265278 {
266279 return RT_NULL ;
@@ -2356,14 +2369,14 @@ void copy(const char *src, const char *dst)
23562369 flag |= FLAG_DST_IS_FILE ;
23572370 }
23582371
2359- // 2. check status
2372+ /* 2. check status */
23602373 if ((flag & FLAG_SRC_IS_DIR ) && (flag & FLAG_DST_IS_FILE ))
23612374 {
23622375 rt_kprintf ("cp faild, cp dir to file is not permitted!\n" );
23632376 return ;
23642377 }
23652378
2366- // 3. do copy
2379+ /* 3. do copy */
23672380 if (flag & FLAG_SRC_IS_FILE )
23682381 {
23692382 if (flag & FLAG_DST_IS_DIR )
@@ -2383,7 +2396,7 @@ void copy(const char *src, const char *dst)
23832396 copyfile (src , dst );
23842397 }
23852398 }
2386- else // flag & FLAG_SRC_IS_DIR
2399+ else /* flag & FLAG_SRC_IS_DIR */
23872400 {
23882401 if (flag & FLAG_DST_IS_DIR )
23892402 {
@@ -2411,4 +2424,4 @@ void copy(const char *src, const char *dst)
24112424}
24122425FINSH_FUNCTION_EXPORT (copy , copy file or dir )
24132426
2414- #endif
2427+ #endif
0 commit comments