sendfile: Fixed behavior of sendfile when count is set to zero.

If sendfile() is called with a zero count, it will nevertheless
try to send the data. This is mostly meaningless, it causes
waste of resources, and in some cases delays.

This commit adds special handling for this case, allowing
sendfile to return immediately zero. The new behavior is
in line with the Linux variant of sendfile.
This commit is contained in:
Fotis Panagiotopoulos 2023-06-19 18:33:17 +03:00 committed by Xiang Xiao
parent 4ae17a6f7b
commit 880d78f903

View file

@ -27,6 +27,7 @@
#include <sys/sendfile.h> #include <sys/sendfile.h>
#include <stdbool.h> #include <stdbool.h>
#include <errno.h> #include <errno.h>
#include <debug.h>
#include <nuttx/kmalloc.h> #include <nuttx/kmalloc.h>
#include <nuttx/net/net.h> #include <nuttx/net/net.h>
@ -244,6 +245,12 @@ static ssize_t copyfile(FAR struct file *outfile, FAR struct file *infile,
ssize_t file_sendfile(FAR struct file *outfile, FAR struct file *infile, ssize_t file_sendfile(FAR struct file *outfile, FAR struct file *infile,
off_t *offset, size_t count) off_t *offset, size_t count)
{ {
if (count == 0)
{
nwarn("WARNING: sendfile count is zero\n");
return 0;
}
#ifdef CONFIG_NET_SENDFILE #ifdef CONFIG_NET_SENDFILE
/* Check the destination file descriptor: Is it a (probable) file /* Check the destination file descriptor: Is it a (probable) file
* descriptor? Check the source file: Is it a normal file? * descriptor? Check the source file: Is it a normal file?