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) {
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;
}
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);
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 */
#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