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

xiaoxiang pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/nuttx.git

commit 1e787ea280d5ebc179f4c39f114ec384e1787d11
Author: Ville Juven <ville.ju...@unikie.com>
AuthorDate: Tue Jun 3 15:56:08 2025 +0800

    nuttx/fs: Reorganize the code for close, dup, et. al.
    
    Currently the code is dumped into one massive file; fs_files. Move the
    different logical parts into their own files.
    
    Signed-off-by: Ville Juven <ville.ju...@unikie.com>
    Signed-off-by: dongjiuzhu1 <dongjiuz...@xiaomi.com>
---
 fs/inode/fs_files.c   | 157 ++------------------------------------------------
 fs/vfs/CMakeLists.txt |   1 +
 fs/vfs/Make.defs      |   4 +-
 fs/vfs/fs_close.c     |  79 +++++++++++++++++++++++++
 fs/vfs/fs_dup2.c      |  47 +++++++++++++++
 fs/vfs/fs_dup3.c      |  60 +++++++++++++++++++
 include/nuttx/fs/fs.h |  21 +++++++
 7 files changed, 215 insertions(+), 154 deletions(-)

diff --git a/fs/inode/fs_files.c b/fs/inode/fs_files.c
index fbe3be4695..d4ecadc91f 100644
--- a/fs/inode/fs_files.c
+++ b/fs/inode/fs_files.c
@@ -46,10 +46,6 @@
 #include <nuttx/spinlock.h>
 #include <nuttx/lib/lib.h>
 
-#ifdef CONFIG_FDSAN
-#  include <android/fdsan.h>
-#endif
-
 #ifdef CONFIG_FDCHECK
 #  include <nuttx/fdcheck.h>
 #endif
@@ -246,6 +242,10 @@ static void task_fssync(FAR struct tcb_s *tcb, FAR void 
*arg)
     }
 }
 
+/****************************************************************************
+ * Public Functions
+ ****************************************************************************/
+
 /****************************************************************************
  * Name: nx_dup3_from_tcb
  *
@@ -266,8 +266,7 @@ static void task_fssync(FAR struct tcb_s *tcb, FAR void 
*arg)
  *
  ****************************************************************************/
 
-static int nx_dup3_from_tcb(FAR struct tcb_s *tcb, int fd1, int fd2,
-                            int flags)
+int nx_dup3_from_tcb(FAR struct tcb_s *tcb, int fd1, int fd2, int flags)
 {
   FAR struct filelist *list;
   FAR struct file *filep1;
@@ -360,10 +359,6 @@ static int nx_dup3_from_tcb(FAR struct tcb_s *tcb, int 
fd1, int fd2,
 #endif
 }
 
-/****************************************************************************
- * Public Functions
- ****************************************************************************/
-
 /****************************************************************************
  * Name: files_initlist
  *
@@ -883,75 +878,6 @@ int nx_dup2_from_tcb(FAR struct tcb_s *tcb, int fd1, int 
fd2)
   return nx_dup3_from_tcb(tcb, fd1, fd2, 0);
 }
 
-/****************************************************************************
- * Name: nx_dup2
- *
- * Description:
- *   nx_dup2() is similar to the standard 'dup2' interface except that is
- *   not a cancellation point and it does not modify the errno variable.
- *
- *   nx_dup2() is an internal NuttX interface and should not be called from
- *   applications.
- *
- *   Clone a file descriptor to a specific descriptor number.
- *
- * Returned Value:
- *   fd2 is returned on success; a negated errno value is return on
- *   any failure.
- *
- ****************************************************************************/
-
-int nx_dup2(int fd1, int fd2)
-{
-  return nx_dup2_from_tcb(this_task(), fd1, fd2);
-}
-
-/****************************************************************************
- * Name: dup2
- *
- * Description:
- *   Clone a file descriptor or socket descriptor to a specific descriptor
- *   number
- *
- ****************************************************************************/
-
-int dup2(int fd1, int fd2)
-{
-  int ret;
-
-  ret = nx_dup2(fd1, fd2);
-  if (ret < 0)
-    {
-      set_errno(-ret);
-      ret = ERROR;
-    }
-
-  return ret;
-}
-
-/****************************************************************************
- * Name: dup3
- *
- * Description:
- *   Clone a file descriptor or socket descriptor to a specific descriptor
- *   number and specific flags.
- *
- ****************************************************************************/
-
-int dup3(int fd1, int fd2, int flags)
-{
-  int ret;
-
-  ret = nx_dup3_from_tcb(this_task(), fd1, fd2, flags);
-  if (ret < 0)
-    {
-      set_errno(-ret);
-      ret = ERROR;
-    }
-
-  return ret;
-}
-
 /****************************************************************************
  * Name: nx_close_from_tcb
  *
@@ -1018,79 +944,6 @@ int nx_close_from_tcb(FAR struct tcb_s *tcb, int fd)
 #endif
 }
 
-/****************************************************************************
- * Name: nx_close
- *
- * Description:
- *   nx_close() is similar to the standard 'close' interface except that is
- *   not a cancellation point and it does not modify the errno variable.
- *
- *   nx_close() is an internal NuttX interface and should not be called from
- *   applications.
- *
- *   Close an inode (if open)
- *
- * Returned Value:
- *   Zero (OK) is returned on success; A negated errno value is returned on
- *   on any failure.
- *
- * Assumptions:
- *   Caller holds the list mutex because the file descriptor will be
- *   freed.
- *
- ****************************************************************************/
-
-int nx_close(int fd)
-{
-  return nx_close_from_tcb(this_task(), fd);
-}
-
-/****************************************************************************
- * Name: close
- *
- * Description:
- *   close() closes a file descriptor, so that it no longer refers to any
- *   file and may be reused. Any record locks (see fcntl(2)) held on the file
- *   it was associated with, and owned by the process, are removed
- *   (regardless of the file descriptor that was used to obtain the lock).
- *
- *   If fd is the last copy of a particular file descriptor the resources
- *   associated with it are freed; if the descriptor was the last reference
- *   to a file which has been removed using unlink(2) the file is deleted.
- *
- * Input Parameters:
- *   fd   file descriptor to close
- *
- * Returned Value:
- *   0 on success; -1 on error with errno set appropriately.
- *
- * Assumptions:
- *
- ****************************************************************************/
-
-int close(int fd)
-{
-  int ret;
-
-#ifdef CONFIG_FDSAN
-  android_fdsan_exchange_owner_tag(fd, 0, 0);
-#endif
-
-  /* close() is a cancellation point */
-
-  enter_cancellation_point();
-
-  ret = nx_close(fd);
-  if (ret < 0)
-    {
-      set_errno(-ret);
-      ret = ERROR;
-    }
-
-  leave_cancellation_point();
-  return ret;
-}
-
 /****************************************************************************
  * Name: sync
  *
diff --git a/fs/vfs/CMakeLists.txt b/fs/vfs/CMakeLists.txt
index c81e615b7c..f261bdfec0 100644
--- a/fs/vfs/CMakeLists.txt
+++ b/fs/vfs/CMakeLists.txt
@@ -25,6 +25,7 @@ set(SRCS
     fs_close.c
     fs_dup.c
     fs_dup2.c
+    fs_dup3.c
     fs_fcntl.c
     fs_epoll.c
     fs_fchstat.c
diff --git a/fs/vfs/Make.defs b/fs/vfs/Make.defs
index 30292d787f..089415c25f 100644
--- a/fs/vfs/Make.defs
+++ b/fs/vfs/Make.defs
@@ -22,8 +22,8 @@
 
 # Common file/socket descriptor support
 
-CSRCS += fs_chstat.c fs_close.c fs_dup.c fs_dup2.c fs_fcntl.c fs_epoll.c
-CSRCS += fs_fchstat.c fs_fstat.c fs_fstatfs.c fs_ioctl.c fs_lseek.c
+CSRCS += fs_chstat.c fs_close.c fs_dup.c fs_dup2.c fs_dup3.c fs_fcntl.c
+CSRCS += fs_epoll.c fs_fchstat.c fs_fstat.c fs_fstatfs.c fs_ioctl.c fs_lseek.c
 CSRCS += fs_mkdir.c fs_open.c fs_poll.c fs_pread.c fs_pwrite.c fs_read.c
 CSRCS += fs_rename.c fs_rmdir.c fs_select.c fs_sendfile.c fs_stat.c
 CSRCS += fs_statfs.c fs_uio.c fs_unlink.c fs_write.c fs_dir.c fs_fsync.c
diff --git a/fs/vfs/fs_close.c b/fs/vfs/fs_close.c
index 6d2f060707..c65c92b00c 100644
--- a/fs/vfs/fs_close.c
+++ b/fs/vfs/fs_close.c
@@ -32,10 +32,16 @@
 #include <errno.h>
 #include <fcntl.h>
 
+#include <nuttx/cancelpt.h>
 #include <nuttx/fs/fs.h>
 
+#ifdef CONFIG_FDSAN
+#  include <android/fdsan.h>
+#endif
+
 #include "notify/notify.h"
 #include "inode/inode.h"
+#include "sched/sched.h"
 #include "vfs/lock.h"
 
 /****************************************************************************
@@ -177,3 +183,76 @@ int file_close(FAR struct file *filep)
 
   return ret;
 }
+
+/****************************************************************************
+ * Name: nx_close
+ *
+ * Description:
+ *   nx_close() is similar to the standard 'close' interface except that is
+ *   not a cancellation point and it does not modify the errno variable.
+ *
+ *   nx_close() is an internal NuttX interface and should not be called from
+ *   applications.
+ *
+ *   Close an inode (if open)
+ *
+ * Returned Value:
+ *   Zero (OK) is returned on success; A negated errno value is returned on
+ *   on any failure.
+ *
+ * Assumptions:
+ *   Caller holds the list mutex because the file descriptor will be
+ *   freed.
+ *
+ ****************************************************************************/
+
+int nx_close(int fd)
+{
+  return nx_close_from_tcb(this_task(), fd);
+}
+
+/****************************************************************************
+ * Name: close
+ *
+ * Description:
+ *   close() closes a file descriptor, so that it no longer refers to any
+ *   file and may be reused. Any record locks (see fcntl(2)) held on the file
+ *   it was associated with, and owned by the process, are removed
+ *   (regardless of the file descriptor that was used to obtain the lock).
+ *
+ *   If fd is the last copy of a particular file descriptor the resources
+ *   associated with it are freed; if the descriptor was the last reference
+ *   to a file which has been removed using unlink(2) the file is deleted.
+ *
+ * Input Parameters:
+ *   fd   file descriptor to close
+ *
+ * Returned Value:
+ *   0 on success; -1 on error with errno set appropriately.
+ *
+ * Assumptions:
+ *
+ ****************************************************************************/
+
+int close(int fd)
+{
+  int ret;
+
+#ifdef CONFIG_FDSAN
+  android_fdsan_exchange_owner_tag(fd, 0, 0);
+#endif
+
+  /* close() is a cancellation point */
+
+  enter_cancellation_point();
+
+  ret = nx_close(fd);
+  if (ret < 0)
+    {
+      set_errno(-ret);
+      ret = ERROR;
+    }
+
+  leave_cancellation_point();
+  return ret;
+}
diff --git a/fs/vfs/fs_dup2.c b/fs/vfs/fs_dup2.c
index bf01133267..ee1e36e576 100644
--- a/fs/vfs/fs_dup2.c
+++ b/fs/vfs/fs_dup2.c
@@ -35,6 +35,7 @@
 #include <fcntl.h>
 
 #include "inode/inode.h"
+#include "sched/sched.h"
 
 /****************************************************************************
  * Public Functions
@@ -197,3 +198,49 @@ int file_dup2(FAR struct file *filep1, FAR struct file 
*filep2)
 {
   return file_dup3(filep1, filep2, 0);
 }
+
+/****************************************************************************
+ * Name: nx_dup2
+ *
+ * Description:
+ *   nx_dup2() is similar to the standard 'dup2' interface except that is
+ *   not a cancellation point and it does not modify the errno variable.
+ *
+ *   nx_dup2() is an internal NuttX interface and should not be called from
+ *   applications.
+ *
+ *   Clone a file descriptor to a specific descriptor number.
+ *
+ * Returned Value:
+ *   fd2 is returned on success; a negated errno value is return on
+ *   any failure.
+ *
+ ****************************************************************************/
+
+int nx_dup2(int fd1, int fd2)
+{
+  return nx_dup2_from_tcb(this_task(), fd1, fd2);
+}
+
+/****************************************************************************
+ * Name: dup2
+ *
+ * Description:
+ *   Clone a file descriptor or socket descriptor to a specific descriptor
+ *   number
+ *
+ ****************************************************************************/
+
+int dup2(int fd1, int fd2)
+{
+  int ret;
+
+  ret = nx_dup2(fd1, fd2);
+  if (ret < 0)
+    {
+      set_errno(-ret);
+      ret = ERROR;
+    }
+
+  return ret;
+}
diff --git a/fs/vfs/fs_dup3.c b/fs/vfs/fs_dup3.c
new file mode 100644
index 0000000000..947b154212
--- /dev/null
+++ b/fs/vfs/fs_dup3.c
@@ -0,0 +1,60 @@
+/****************************************************************************
+ * fs/vfs/fs_dup3.c
+ *
+ * SPDX-License-Identifier: Apache-2.0
+ *
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.  The
+ * ASF licenses this file to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance with the
+ * License.  You may obtain a copy of the License at
+ *
+ *   http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
+ * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.  See the
+ * License for the specific language governing permissions and limitations
+ * under the License.
+ *
+ ****************************************************************************/
+
+/****************************************************************************
+ * Included Files
+ ****************************************************************************/
+
+#include <nuttx/fs/fs.h>
+
+#include <unistd.h>
+#include <sched.h>
+#include <errno.h>
+
+#include "sched/sched.h"
+
+/****************************************************************************
+ * Public Functions
+ ****************************************************************************/
+
+/****************************************************************************
+ * Name: dup3
+ *
+ * Description:
+ *   Clone a file descriptor or socket descriptor to a specific descriptor
+ *   number and specific flags.
+ *
+ ****************************************************************************/
+
+int dup3(int fd1, int fd2, int flags)
+{
+  int ret;
+
+  ret = nx_dup3_from_tcb(this_task(), fd1, fd2, flags);
+  if (ret < 0)
+    {
+      set_errno(-ret);
+      ret = ERROR;
+    }
+
+  return ret;
+}
diff --git a/include/nuttx/fs/fs.h b/include/nuttx/fs/fs.h
index 4e73f8b2c9..d169aac287 100644
--- a/include/nuttx/fs/fs.h
+++ b/include/nuttx/fs/fs.h
@@ -1027,6 +1027,27 @@ int file_dup(FAR struct file *filep, int minfd, int 
flags);
 
 int file_dup2(FAR struct file *filep1, FAR struct file *filep2);
 
+/****************************************************************************
+ * Name: nx_dup3_from_tcb
+ *
+ * Description:
+ *   nx_dup3_from_tcb() is similar to the standard 'dup3' interface
+ *   except that is not a cancellation point and it does not modify the
+ *   errno variable.
+ *
+ *   nx_dup3_from_tcb() is an internal NuttX interface and should not be
+ *   called from applications.
+ *
+ *   Clone a file descriptor to a specific descriptor number.
+ *
+ * Returned Value:
+ *   fd2 is returned on success; a negated errno value is return on
+ *   any failure.
+ *
+ ****************************************************************************/
+
+int nx_dup3_from_tcb(FAR struct tcb_s *tcb, int fd1, int fd2, int flags);
+
 /****************************************************************************
  * Name: nx_dup2_from_tcb
  *

Reply via email to