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.git

commit 935f43185f590836c11f272481f804e9b46bdc65
Author: Xiang Xiao <xiaoxi...@xiaomi.com>
AuthorDate: Tue Jul 4 19:58:04 2023 +0800

    libc/modlib: Replace nx_stat with file_stat
    
    to avoid searching the path twice
    
    Signed-off-by: Xiang Xiao <xiaoxi...@xiaomi.com>
---
 fs/vfs/fs_fstat.c              | 47 +++++++++++++++++++++---------------------
 include/nuttx/fs/fs.h          | 21 +++++++++++++++++--
 libs/libc/modlib/modlib_init.c | 27 ++++++++++--------------
 3 files changed, 54 insertions(+), 41 deletions(-)

diff --git a/fs/vfs/fs_fstat.c b/fs/vfs/fs_fstat.c
index c0d17dd22a..08d7188792 100644
--- a/fs/vfs/fs_fstat.c
+++ b/fs/vfs/fs_fstat.c
@@ -222,6 +222,26 @@ int file_fstat(FAR struct file *filep, FAR struct stat 
*buf)
   return ret;
 }
 
+int nx_fstat(int fd, FAR struct stat *buf)
+{
+  FAR struct file *filep;
+  int ret;
+
+  /* First, get the file structure.  Note that on failure,
+   * fs_getfilep() will return the errno.
+   */
+
+  ret = fs_getfilep(fd, &filep);
+  if (ret >= 0)
+    {
+      /* Perform the fstat operation */
+
+      return file_fstat(filep, buf);
+    }
+
+  return ret;
+}
+
 /****************************************************************************
  * Name: fstat
  *
@@ -246,33 +266,14 @@ int file_fstat(FAR struct file *filep, FAR struct stat 
*buf)
 
 int fstat(int fd, FAR struct stat *buf)
 {
-  FAR struct file *filep;
   int ret;
 
-  /* First, get the file structure.  Note that on failure,
-   * fs_getfilep() will return the errno.
-   */
-
-  ret = fs_getfilep(fd, &filep);
+  ret = nx_fstat(fd, buf);
   if (ret < 0)
     {
-      goto errout;
-    }
-
-  /* Perform the fstat operation */
-
-  ret = file_fstat(filep, buf);
-
-  /* Check if the fstat operation was successful */
-
-  if (ret >= 0)
-    {
-      /* Successfully fstat'ed the file */
-
-      return OK;
+      set_errno(-ret);
+      ret = ERROR;
     }
 
-errout:
-  set_errno(-ret);
-  return ERROR;
+  return ret;
 }
diff --git a/include/nuttx/fs/fs.h b/include/nuttx/fs/fs.h
index 17410a5ba6..a9012eb1a2 100644
--- a/include/nuttx/fs/fs.h
+++ b/include/nuttx/fs/fs.h
@@ -74,7 +74,7 @@
 #  define _NX_READ(f,b,s)      nx_read(f,b,s)
 #  define _NX_WRITE(f,b,s)     nx_write(f,b,s)
 #  define _NX_SEEK(f,o,w)      nx_seek(f,o,w)
-#  define _NX_STAT(p,s)        nx_stat(p,s,1)
+#  define _NX_STAT(f,s)        nx_fstat(f,s)
 #  define _NX_GETERRNO(r)      (-(r))
 #  define _NX_SETERRNO(r)      set_errno(-(r))
 #  define _NX_GETERRVAL(r)     (r)
@@ -84,7 +84,7 @@
 #  define _NX_READ(f,b,s)      read(f,b,s)
 #  define _NX_WRITE(f,b,s)     write(f,b,s)
 #  define _NX_SEEK(f,o,w)      lseek(f,o,w)
-#  define _NX_STAT(p,s)        stat(p,s)
+#  define _NX_STAT(f,s)        fstat(f,s)
 #  define _NX_GETERRNO(r)      errno
 #  define _NX_SETERRNO(r)      ((void)(r))
 #  define _NX_GETERRVAL(r)     (-errno)
@@ -1458,6 +1458,23 @@ int file_poll(FAR struct file *filep, FAR struct pollfd 
*fds, bool setup);
 
 int file_fstat(FAR struct file *filep, FAR struct stat *buf);
 
+/****************************************************************************
+ * Name: nx_fstat
+ *
+ * Description:
+ *   nx_fstat() is similar to the standard 'fstat' interface except that is
+ *   not a cancellation point and it does not modify the errno variable.
+ *
+ *   nx_fstat() is an internal NuttX interface and should not be called from
+ *   applications.
+ *
+ * Returned Value:
+ *   Zero is returned on success; a negated value is returned on any failure.
+ *
+ ****************************************************************************/
+
+int nx_fstat(int fd, FAR struct stat *buf);
+
 /****************************************************************************
  * Name: nx_stat
  *
diff --git a/libs/libc/modlib/modlib_init.c b/libs/libc/modlib/modlib_init.c
index d26efca8c1..e8cd643bea 100644
--- a/libs/libc/modlib/modlib_init.c
+++ b/libs/libc/modlib/modlib_init.c
@@ -71,15 +71,14 @@
  *
  ****************************************************************************/
 
-static inline int modlib_filelen(FAR struct mod_loadinfo_s *loadinfo,
-                                 FAR const char *filename)
+static inline int modlib_filelen(FAR struct mod_loadinfo_s *loadinfo)
 {
   struct stat buf;
   int ret;
 
   /* Get the file stats */
 
-  ret = _NX_STAT(filename, &buf);
+  ret = _NX_STAT(loadinfo->filfd, &buf);
   if (ret < 0)
     {
       int errval = _NX_GETERRNO(ret);
@@ -95,10 +94,6 @@ static inline int modlib_filelen(FAR struct mod_loadinfo_s 
*loadinfo,
       return -ENOENT;
     }
 
-  /* TODO:  Verify that the file is readable.  Not really important because
-   * we will detect this when we try to open the file read-only.
-   */
-
   /* Return the size of the file in the loadinfo structure */
 
   loadinfo->filelen = buf.st_size;
@@ -134,15 +129,6 @@ int modlib_initialize(FAR const char *filename,
   memset(loadinfo, 0, sizeof(struct mod_loadinfo_s));
   loadinfo->filfd = -1;
 
-  /* Get the length of the file. */
-
-  ret = modlib_filelen(loadinfo, filename);
-  if (ret < 0)
-    {
-      berr("ERROR: modlib_filelen failed: %d\n", ret);
-      return ret;
-    }
-
   /* Open the binary file for reading (only) */
 
   loadinfo->filfd = _NX_OPEN(filename, O_RDONLY);
@@ -153,6 +139,15 @@ int modlib_initialize(FAR const char *filename,
       return -errval;
     }
 
+  /* Get the length of the file. */
+
+  ret = modlib_filelen(loadinfo);
+  if (ret < 0)
+    {
+      berr("ERROR: modlib_filelen failed: %d\n", ret);
+      return ret;
+    }
+
   /* Read the ELF ehdr from offset 0 */
 
   ret = modlib_read(loadinfo, (FAR uint8_t *)&loadinfo->ehdr,

Reply via email to