From 880d78f903a7506cdcaa9de38b1e22b04ecb707b Mon Sep 17 00:00:00 2001 From: Fotis Panagiotopoulos Date: Mon, 19 Jun 2023 18:33:17 +0300 Subject: [PATCH] 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. --- fs/vfs/fs_sendfile.c | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/fs/vfs/fs_sendfile.c b/fs/vfs/fs_sendfile.c index 5b4ec2cff5..ac460a63bf 100644 --- a/fs/vfs/fs_sendfile.c +++ b/fs/vfs/fs_sendfile.c @@ -27,6 +27,7 @@ #include #include #include +#include #include #include @@ -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, off_t *offset, size_t count) { + if (count == 0) + { + nwarn("WARNING: sendfile count is zero\n"); + return 0; + } + #ifdef CONFIG_NET_SENDFILE /* Check the destination file descriptor: Is it a (probable) file * descriptor? Check the source file: Is it a normal file?