This is an automated email from the ASF dual-hosted git repository.
GUIDINGLI pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/nuttx-apps.git
The following commit(s) were added to refs/heads/master by this push:
new 6afd27088 nshlib: handle directory destinations in mv
6afd27088 is described below
commit 6afd27088f0dae9208a2897f154c9b206b50da24
Author: jingfei <[email protected]>
AuthorDate: Fri Nov 27 04:29:10 2026 +0800
nshlib: handle directory destinations in mv
Append the source basename when mv targets an existing directory.
Passing that directory straight to rename can replace the directory or
fail with EISDIR instead of moving the source inside it.
Build the child path without resolving its terminal component so an
existing symbolic link there is replaced rather than followed. Keep
the original source path and release temporary paths on all exits.
Signed-off-by: jingfei <[email protected]>
---
nshlib/nsh_fscmds.c | 30 ++++++++++++++++++++++++++++--
1 file changed, 28 insertions(+), 2 deletions(-)
diff --git a/nshlib/nsh_fscmds.c b/nshlib/nsh_fscmds.c
index c30c03f35..5a8520cdc 100644
--- a/nshlib/nsh_fscmds.c
+++ b/nshlib/nsh_fscmds.c
@@ -2549,6 +2549,8 @@ int cmd_mv(FAR struct nsh_vtbl_s *vtbl, int argc, FAR
char **argv)
FAR char *oldpath;
FAR char *newpath;
+ FAR char *target = NULL;
+ struct stat st;
int ret;
/* Get the full path to the old and new file paths */
@@ -2568,9 +2570,30 @@ int cmd_mv(FAR struct nsh_vtbl_s *vtbl, int argc, FAR
char **argv)
goto errout_with_oldpath;
}
- /* Perform the mount */
+ if (stat(newpath, &st) == 0 && S_ISDIR(st.st_mode))
+ {
+ FAR char *name = strdup(oldpath);
+
+ if (name == NULL)
+ {
+ nsh_error(vtbl, g_fmtcmdoutofmemory, argv[0]);
+ ret = ERROR;
+ goto errout_with_newpath;
+ }
+
+ /* Keep a terminal destination symlink for rename() to replace. */
- ret = rename(oldpath, newpath);
+ ret = asprintf(&target, "%s/%s", newpath, basename(name));
+ free(name);
+ if (ret < 0)
+ {
+ nsh_error(vtbl, g_fmtcmdoutofmemory, argv[0]);
+ ret = ERROR;
+ goto errout_with_newpath;
+ }
+ }
+
+ ret = rename(oldpath, target != NULL ? target : newpath);
if (ret < 0)
{
nsh_error(vtbl, g_fmtcmdfailed, argv[0], "rename", NSH_ERRNO);
@@ -2578,6 +2601,9 @@ int cmd_mv(FAR struct nsh_vtbl_s *vtbl, int argc, FAR
char **argv)
/* Free the file paths */
+ free(target);
+
+errout_with_newpath:
nsh_freefullpath(newpath);
errout_with_oldpath: