]> git.ipfire.org Git - thirdparty/dovecot/core.git/commitdiff
lib: safe_sendfile() - clarify return value 0 handling
authorTimo Sirainen <timo.sirainen@open-xchange.com>
Thu, 12 Mar 2020 10:57:41 +0000 (12:57 +0200)
committertimo.sirainen <timo.sirainen@open-xchange.com>
Fri, 27 Mar 2020 14:35:20 +0000 (14:35 +0000)
src/lib/sendfile-util.c
src/lib/sendfile-util.h

index 83e420af3fb1716351f0d0571a76268730910b92..662285b14d35e2b19a429db2b0e2426c0bd75dc5 100644 (file)
@@ -22,8 +22,7 @@ ssize_t safe_sendfile(int out_fd, int in_fd, uoff_t *offset, size_t count)
        off_t safe_offset;
        ssize_t ret;
 
-       if (count == 0)
-               return 0;
+       i_assert(count > 0);
 
        /* make sure given offset fits into off_t */
        if (sizeof(off_t) * CHAR_BIT == 32) {
@@ -49,7 +48,7 @@ ssize_t safe_sendfile(int out_fd, int in_fd, uoff_t *offset, size_t count)
 
        safe_offset = (off_t)*offset;
        ret = sendfile(out_fd, in_fd, &safe_offset, count);
-       /* ret=0 : trying to read past EOF, errno = EPIPE : remote is gone */
+       /* ret=0 : trying to read past EOF */
        *offset = (uoff_t)safe_offset;
        return ret;
 }
@@ -65,14 +64,11 @@ ssize_t safe_sendfile(int out_fd, int in_fd, uoff_t *offset, size_t count)
        off_t sbytes;
        int ret;
 
+       /* if count=0 is passed to sendfile(), it sends everything
+          from in_fd until EOF. We don't want that. */
+       i_assert(count > 0);
        i_assert(count <= SSIZE_T_MAX);
 
-       if (count == 0) {
-               /* if count=0 is passed to sendfile(), it sends everything
-                  from in_fd until EOF. We don't want that. */
-               return 0;
-       }
-
        i_zero(&hdtr);
        ret = sendfile(in_fd, out_fd, *offset, count, &hdtr, &sbytes, 0);
 
@@ -100,11 +96,9 @@ ssize_t safe_sendfile(int out_fd, int in_fd, uoff_t *offset, size_t count)
        ssize_t ret;
        off_t s_offset;
 
+       i_assert(count > 0);
        i_assert(count <= SSIZE_T_MAX);
 
-       if (count == 0)
-               return 0;
-
        /* NOTE: if outfd is not a socket, some Solaris versions will
           kernel panic */
 
index 29ada6f605f89e10a1cff0cd221a852941e2effe..3d7f14aa0aaa24a7d0bd7398df33790498765194 100644 (file)
@@ -1,9 +1,16 @@
 #ifndef SENDFILE_UTIL_H
 #define SENDFILE_UTIL_H
 
-/* Wrapper for various sendfile()-like calls. Returns -1 and errno=EINVAL if
-   it isn't supported for some reason (out_fd isn't a socket, offset is too
-   large, or there simply is no sendfile()). */
+/* Wrapper for various sendfile()-like calls. Read a maximum of count bytes
+   from the given offset in in_fd and write them to out_fd. The offset is
+   updated after the call. Note the call assert-crashes if count is 0.
+
+   Returns:
+   >0 number of bytes successfully written (maybe less than count)
+   0 if offset points to the input's EOF or past it
+   -1, errno=EINVAL if it isn't supported for some reason (out_fd isn't a
+       socket or there simply is no sendfile()).
+   -1, errno=EAGAIN if non-blocking write couldn't send anything */
 ssize_t safe_sendfile(int out_fd, int in_fd, uoff_t *offset, size_t count);
 
 #endif