This is an automated email from the ASF dual-hosted git repository.
xiaoxiang781216 pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/nuttx.git
The following commit(s) were added to refs/heads/master by this push:
new d0efca238ec fs/vfs: Return the count of a partial sendfile().
d0efca238ec is described below
commit d0efca238ec00be9924a46a4a0b15bdcb53e0820
Author: Marco Casaroli <[email protected]>
AuthorDate: Tue Nov 17 10:46:43 2026 +0100
fs/vfs: Return the count of a partial sendfile().
copyfile() is the sendfile() path for every output except a socket with
CONFIG_NET_SENDFILE. When a read or a write failed after some bytes
went out, for example with EAGAIN on a full nonblocking socket or pipe,
copyfile() returned the error and not the count. Also *offset was the
input position after the last block read, past the bytes that were
written. Without an offset, the file position stayed there. A caller
that continues from the count or from *offset after EAGAIN then sends
some bytes twice or skips some.
Return the count when some bytes went out, and the error only when no
byte went out, as write() does. Move *offset by the count. Without an
offset, seek the input back over the bytes that were read but not
written. An input that cannot seek loses these bytes, as before.
Assisted-by: Claude Code:claude-opus-5-5
Signed-off-by: Marco Casaroli <[email protected]>
---
fs/vfs/fs_sendfile.c | 47 +++++++++++++++++++++++++++--------------------
1 file changed, 27 insertions(+), 20 deletions(-)
diff --git a/fs/vfs/fs_sendfile.c b/fs/vfs/fs_sendfile.c
index add5224ba0b..3c5e2717b16 100644
--- a/fs/vfs/fs_sendfile.c
+++ b/fs/vfs/fs_sendfile.c
@@ -46,8 +46,9 @@ static ssize_t copyfile(FAR struct file *outfile, FAR struct
file *infile,
FAR uint8_t *iobuffer;
FAR uint8_t *wrbuffer;
off_t startpos = 0;
- ssize_t nbytesread;
+ ssize_t nbytesread = 0;
ssize_t nbyteswritten;
+ ssize_t ret = 0;
size_t ntransferred;
bool endxfr;
@@ -124,10 +125,12 @@ static ssize_t copyfile(FAR struct file *outfile, FAR
struct file *infile,
if (nbytesread != -EINTR || ntransferred == 0)
{
- /* Read error. Break out and return the error condition. */
+ /* Read error. Break out and return the error condition
+ * if nothing was transferred.
+ */
- ntransferred = nbytesread;
- endxfr = true;
+ ret = nbytesread;
+ endxfr = true;
break;
}
}
@@ -184,11 +187,11 @@ static ssize_t copyfile(FAR struct file *outfile, FAR
struct file *infile,
if (nbyteswritten != -EINTR || ntransferred == 0)
{
/* Write error. Break out and return the error
- * condition.
+ * condition if nothing was transferred.
*/
- ntransferred = nbyteswritten;
- endxfr = true;
+ ret = nbyteswritten;
+ endxfr = true;
break;
}
}
@@ -205,18 +208,9 @@ static ssize_t copyfile(FAR struct file *outfile, FAR
struct file *infile,
if (offset)
{
- /* Use file_seek to get the current file position */
-
- off_t curpos = file_seek(infile, 0, SEEK_CUR);
-
- if (curpos < 0)
- {
- return curpos;
- }
+ /* Return the file position after the last byte transferred */
- /* Return the current file position */
-
- *offset = curpos;
+ *offset += ntransferred;
/* Use file_seek again to restore the original file position */
@@ -226,12 +220,25 @@ static ssize_t copyfile(FAR struct file *outfile, FAR
struct file *infile,
return startpos;
}
}
+ else if (nbytesread > 0)
+ {
+ /* Give back the bytes read but not written. An input that cannot
+ * seek loses them.
+ */
+
+ file_seek(infile, -nbytesread, SEEK_CUR);
+ }
/* Finally return the number of bytes actually transferred (or ERROR
- * if any failure occurred).
+ * if no data was transferred).
*/
- return ntransferred;
+ if (ntransferred > 0)
+ {
+ return ntransferred;
+ }
+
+ return ret;
}
/****************************************************************************