You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@nuttx.apache.org by ac...@apache.org on 2026/10/18 12:11:04 UTC

(nuttx-apps) branch master updated: nshlib: Keep the execute permission of the source in cp.

This is an automated email from the ASF dual-hosted git repository.

acassis 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 1946ca05a nshlib: Keep the execute permission of the source in cp.
1946ca05a is described below

commit 1946ca05adaf4f8efc3458b4cda9164a54eccf04
Author: Marco Casaroli <ma...@gmail.com>
AuthorDate: Sun Oct 18 00:09:17 2026 +0200

    nshlib: Keep the execute permission of the source in cp.
    
    cp created the new file with mode 0666.  On a file system that keeps the
    mode, a copied program lost its execute permission.
    
    Create the new file with 0666 plus the execute bits of the source.  The
    mode of an existing destination file does not change.
    
    Assisted-by: Claude Code:claude-opus-5-5
    Signed-off-by: Marco Casaroli <ma...@gmail.com>
---
 nshlib/nsh_fscmds.c | 10 +++++++++-
 1 file changed, 9 insertions(+), 1 deletion(-)

diff --git a/nshlib/nsh_fscmds.c b/nshlib/nsh_fscmds.c
index 42c72d375..c30c03f35 100644
--- a/nshlib/nsh_fscmds.c
+++ b/nshlib/nsh_fscmds.c
@@ -118,6 +118,7 @@ static int cp_handler(FAR struct nsh_vtbl_s *vtbl, FAR const char *srcpath,
   struct stat buf;
   FAR char *allocpath = NULL;
   int oflags = O_WRONLY | O_CREAT | O_TRUNC;
+  mode_t mode = 0666;
   int rdfd;
   int wrfd;
   int ret = ERROR;
@@ -129,6 +130,13 @@ static int cp_handler(FAR struct nsh_vtbl_s *vtbl, FAR const char *srcpath,
       return ret;
     }
 
+  /* A new file gets the execute permission of the source file */
+
+  if (fstat(rdfd, &buf) == 0)
+    {
+      mode |= buf.st_mode & (S_IXUSR | S_IXGRP | S_IXOTH);
+    }
+
   /* Check if the destination is a directory */
 
   if (stat(destpath, &buf) == 0)
@@ -165,7 +173,7 @@ static int cp_handler(FAR struct nsh_vtbl_s *vtbl, FAR const char *srcpath,
         }
     }
 
-  wrfd = open(destpath, oflags, 0666);
+  wrfd = open(destpath, oflags, mode);
   if (wrfd < 0)
     {
       nsh_error(vtbl, g_fmtcmdfailed, "cp", "open_wrfd", NSH_ERRNO);