You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@nuttx.apache.org by xi...@apache.org on 2022/01/18 02:39:01 UTC

[incubator-nuttx-apps] branch master updated: netutils/netcat: sendfile related code refactoring + small fixes

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/incubator-nuttx-apps.git


The following commit(s) were added to refs/heads/master by this push:
     new 371beb2  netutils/netcat: sendfile related code refactoring + small fixes
371beb2 is described below

commit 371beb2a0fed0b069fd1c2eb05f157c3009be1ee
Author: Alexander Lunev <al...@mail.ru>
AuthorDate: Tue Jan 18 03:29:03 2022 +0300

    netutils/netcat: sendfile related code refactoring + small fixes
---
 netutils/netcat/netcat_main.c | 51 ++++++++++++++++++++++++-------------------
 1 file changed, 29 insertions(+), 22 deletions(-)

diff --git a/netutils/netcat/netcat_main.c b/netutils/netcat/netcat_main.c
index a345afa..da32011 100644
--- a/netutils/netcat/netcat_main.c
+++ b/netutils/netcat/netcat_main.c
@@ -78,6 +78,33 @@ int do_io(int infd, int outfd)
   return EXIT_SUCCESS;
 }
 
+#ifdef CONFIG_NETUTILS_NETCAT_SENDFILE
+int do_io_over_sendfile(int infd, int outfd, ssize_t len)
+{
+  off_t offset = 0;
+  ssize_t written;
+
+  while (len > 0)
+    {
+      written = sendfile(outfd, infd, &offset, len);
+
+      if (written == -1 && errno == EAGAIN)
+        {
+          continue;
+        }
+      else if (written == -1)
+        {
+          perror("do_io: sendfile error");
+          return 5;
+        }
+
+      len -= written;
+    }
+
+  return EXIT_SUCCESS;
+}
+#endif
+
 int netcat_server(int argc, char * argv[])
 {
   int id = -1;
@@ -170,8 +197,6 @@ int netcat_client(int argc, char * argv[])
   int result = EXIT_SUCCESS;
 #ifdef CONFIG_NETUTILS_NETCAT_SENDFILE
   struct stat stat_buf;
-  off_t offset;
-  ssize_t len;
 #endif
 
   if (argc > 1)
@@ -198,7 +223,7 @@ int netcat_client(int argc, char * argv[])
 #ifdef CONFIG_NETUTILS_NETCAT_SENDFILE
       if (fstat(infd, &stat_buf) == -1)
         {
-          perror("fstat");
+          perror("error: fstat: Could not get the input file size");
           infd = STDIN_FILENO;
           result = 1;
           goto out;
@@ -234,25 +259,7 @@ int netcat_client(int argc, char * argv[])
 #ifdef CONFIG_NETUTILS_NETCAT_SENDFILE
   if (argc > 3)
     {
-      len = stat_buf.st_size;
-      offset = 0;
-
-      while (len > 0)
-        {
-          result = sendfile(id, infd, &offset, len);
-
-          if (result == -1 && errno == EAGAIN)
-            {
-              continue;
-            }
-          else if (result == -1)
-            {
-              perror("sendfile error");
-              break;
-            }
-
-          len -= result;
-        }
+      result = do_io_over_sendfile(infd, id, stat_buf.st_size);
     }
   else
 #endif