You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@nuttx.apache.org by gu...@apache.org on 2021/07/17 10:39:23 UTC

[incubator-nuttx] 01/02: net: Add file_socket function

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

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

commit 2e0901fcaa684d7a1805638437a1ec895d616267
Author: Xiang Xiao <xi...@xiaomi.com>
AuthorDate: Sat Jul 10 23:19:30 2021 +0800

    net: Add file_socket function
    
    and move the socket special process from fstat/nx_vfcntl/ to file_fstat/file_vfcntl
    
    Signed-off-by: Xiang Xiao <xi...@xiaomi.com>
    Change-Id: Ia10341538488ba3a8444df8e73fb5257b2a1f512
---
 fs/socket/socket.c      | 17 +++++++++++------
 fs/vfs/fs_fcntl.c       | 38 ++++++++++++++++++--------------------
 fs/vfs/fs_fstat.c       | 24 +++++++++---------------
 include/nuttx/net/net.h |  5 ++---
 4 files changed, 40 insertions(+), 44 deletions(-)

diff --git a/fs/socket/socket.c b/fs/socket/socket.c
index 69718cb..a07c11c 100644
--- a/fs/socket/socket.c
+++ b/fs/socket/socket.c
@@ -187,6 +187,16 @@ int sockfd_allocate(FAR struct socket *psock, int oflags)
  *
  ****************************************************************************/
 
+FAR struct socket *file_socket(FAR struct file *filep)
+{
+  if (filep != NULL && INODE_IS_SOCKET(filep->f_inode))
+    {
+      return filep->f_priv;
+    }
+
+  return NULL;
+}
+
 FAR struct socket *sockfd_socket(int sockfd)
 {
   FAR struct file *filep;
@@ -196,12 +206,7 @@ FAR struct socket *sockfd_socket(int sockfd)
       return NULL;
     }
 
-  if (INODE_IS_SOCKET(filep->f_inode))
-    {
-      return filep->f_priv;
-    }
-
-  return NULL;
+  return file_socket(filep);
 }
 
 /****************************************************************************
diff --git a/fs/vfs/fs_fcntl.c b/fs/vfs/fs_fcntl.c
index 6577422..e38eef6 100644
--- a/fs/vfs/fs_fcntl.c
+++ b/fs/vfs/fs_fcntl.c
@@ -56,6 +56,20 @@ static int file_vfcntl(FAR struct file *filep, int cmd, va_list ap)
       return -EBADF;
     }
 
+  /* check for operations on a socket descriptor */
+
+#ifdef CONFIG_NET
+  if (INODE_IS_SOCKET(filep->f_inode) &&
+      cmd != F_DUPFD && cmd != F_GETFD && cmd != F_SETFD)
+    {
+      /* Yes.. defer socket descriptor operations to
+       * psock_vfcntl(). The errno is not set on failures.
+       */
+
+      return psock_vfcntl(file_socket(filep), cmd, ap);
+    }
+#endif
+
   switch (cmd)
     {
       case F_DUPFD:
@@ -239,27 +253,11 @@ static int nx_vfcntl(int fd, int cmd, va_list ap)
     {
       DEBUGASSERT(filep != NULL);
 
-      /* check for operations on a socket descriptor */
+      /* Let file_vfcntl() do the real work.  The errno is not set on
+       * failures.
+       */
 
-#ifdef CONFIG_NET
-      if (INODE_IS_SOCKET(filep->f_inode) &&
-          cmd != F_DUPFD && cmd != F_GETFD && cmd != F_SETFD)
-        {
-          /* Yes.. defer socket descriptor operations to
-           * psock_vfcntl(). The errno is not set on failures.
-           */
-
-          ret = psock_vfcntl(sockfd_socket(fd), cmd, ap);
-        }
-      else
-#endif
-        {
-          /* Let file_vfcntl() do the real work.  The errno is not set on
-           * failures.
-           */
-
-          ret = file_vfcntl(filep, cmd, ap);
-        }
+      ret = file_vfcntl(filep, cmd, ap);
     }
 
   return ret;
diff --git a/fs/vfs/fs_fstat.c b/fs/vfs/fs_fstat.c
index a3e858e..dbf4cbc 100644
--- a/fs/vfs/fs_fstat.c
+++ b/fs/vfs/fs_fstat.c
@@ -192,6 +192,15 @@ int file_fstat(FAR struct file *filep, FAR struct stat *buf)
     }
   else
 #endif
+#ifdef CONFIG_NET
+  if (INODE_IS_SOCKET(inode))
+    {
+      /* Let the networking logic handle the fstat() */
+
+      ret = psock_fstat(file_socket(filep), buf);
+    }
+  else
+#endif
     {
       /* Check if the inode is a proxy for a block or MTD driver */
 
@@ -244,21 +253,6 @@ int fstat(int fd, FAR struct stat *buf)
       goto errout;
     }
 
-#ifdef CONFIG_NET
-  if (INODE_IS_SOCKET(filep->f_inode))
-    {
-      /* Let the networking logic handle the fstat() */
-
-      ret = psock_fstat(sockfd_socket(fd), buf);
-      if (ret < 0)
-        {
-          goto errout;
-        }
-
-      return OK;
-    }
-#endif
-
   /* Perform the fstat operation */
 
   ret = file_fstat(filep, buf);
diff --git a/include/nuttx/net/net.h b/include/nuttx/net/net.h
index fb31d93..d79162e 100644
--- a/include/nuttx/net/net.h
+++ b/include/nuttx/net/net.h
@@ -168,6 +168,7 @@ typedef uint8_t sockcaps_t;
  */
 
 struct file;    /* Forward reference */
+struct stat;    /* Forward reference */
 struct socket;  /* Forward reference */
 struct pollfd;  /* Forward reference */
 
@@ -521,6 +522,7 @@ int sockfd_allocate(FAR struct socket *psock, int oflags);
  *
  ****************************************************************************/
 
+FAR struct socket *file_socket(FAR struct file *filep);
 FAR struct socket *sockfd_socket(int sockfd);
 
 /****************************************************************************
@@ -1312,8 +1314,6 @@ int psock_dup2(FAR struct socket *psock1, FAR struct socket *psock2);
  *
  ****************************************************************************/
 
-struct stat;  /* Forward reference.  See sys/stat.h */
-
 int psock_fstat(FAR struct socket *psock, FAR struct stat *buf);
 
 /****************************************************************************
@@ -1379,7 +1379,6 @@ int psock_fstat(FAR struct socket *psock, FAR struct stat *buf);
  ****************************************************************************/
 
 #ifdef CONFIG_NET_SENDFILE
-struct file;
 ssize_t psock_sendfile(FAR struct socket *psock, FAR struct file *infile,
                        FAR off_t *offset, size_t count);
 #endif