From 371beb2a0fed0b069fd1c2eb05f157c3009be1ee Mon Sep 17 00:00:00 2001 From: Alexander Lunev Date: Tue, 18 Jan 2022 03:29:03 +0300 Subject: [PATCH] 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 a345afac3..da320115b 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