cleanup and add fallback to read/write if no sendfile support.
This commit is contained in:
parent
2336926d90
commit
243c780a6a
|
@ -3,37 +3,44 @@
|
||||||
|
|
||||||
#ifdef HAVE_SENDFILE4_SUPPORT
|
#ifdef HAVE_SENDFILE4_SUPPORT
|
||||||
#include <sys/sendfile.h>
|
#include <sys/sendfile.h>
|
||||||
ssize_t zm_sendfile(int out_fd, int in_fd, off_t *offset, size_t size) {
|
|
||||||
size_t remaining = size;
|
|
||||||
while (remaining) {
|
|
||||||
ssize_t err = sendfile(out_fd, in_fd, offset, remaining);
|
|
||||||
if (err < 0) {
|
|
||||||
return -errno;
|
|
||||||
}
|
|
||||||
remaining -= err;
|
|
||||||
offset += err;
|
|
||||||
}
|
|
||||||
|
|
||||||
return size-remaining;
|
|
||||||
}
|
|
||||||
#elif HAVE_SENDFILE7_SUPPORT
|
#elif HAVE_SENDFILE7_SUPPORT
|
||||||
#include <sys/types.h>
|
#include <sys/types.h>
|
||||||
#include <sys/socket.h>
|
#include <sys/socket.h>
|
||||||
#include <sys/uio.h>
|
#include <sys/uio.h>
|
||||||
ssize_t zm_sendfile(int out_fd, int in_fd, off_t *offset, off_t size) {
|
#else
|
||||||
|
#include <unistd.h>
|
||||||
|
#endif
|
||||||
|
|
||||||
|
/* Function to send the contents of a file. Will use sendfile or fall back to reading/writing */
|
||||||
|
|
||||||
|
ssize_t zm_sendfile(int out_fd, int in_fd, off_t *offset, size_t size) {
|
||||||
|
#ifdef HAVE_SENDFILE4_SUPPORT
|
||||||
|
ssize_t err = sendfile(out_fd, in_fd, offset, size);
|
||||||
|
if (err < 0) {
|
||||||
|
return -errno;
|
||||||
|
}
|
||||||
|
return err;
|
||||||
|
|
||||||
|
#elif HAVE_SENDFILE7_SUPPORT
|
||||||
ssize_t err = sendfile(in_fd, out_fd, *offset, size, nullptr, &size, 0);
|
ssize_t err = sendfile(in_fd, out_fd, *offset, size, nullptr, &size, 0);
|
||||||
if (err && errno != EAGAIN)
|
if (err && errno != EAGAIN)
|
||||||
return -errno;
|
return -errno;
|
||||||
|
|
||||||
if (size) {
|
|
||||||
*offset += size;
|
|
||||||
return size;
|
return size;
|
||||||
|
#else
|
||||||
|
uint8_t buffer[size];
|
||||||
|
ssize_t err = read(in_fd, buffer, size);
|
||||||
|
if (err < 0) {
|
||||||
|
Error("Unable to read %zu bytes: %s", size, strerror(errno));
|
||||||
|
return -errno;
|
||||||
}
|
}
|
||||||
|
|
||||||
return -EAGAIN;
|
err = fwrite(out_fd, buffer, size);
|
||||||
}
|
if (err < 0) {
|
||||||
#else
|
Error("Unable to write %zu bytes: %s", size, strerror(errno));
|
||||||
#error "Your platform does not support sendfile. Sorry."
|
return -errno;
|
||||||
|
}
|
||||||
|
return err;
|
||||||
#endif
|
#endif
|
||||||
|
}
|
||||||
|
|
||||||
#endif // ZM_SENDFILE_H
|
#endif // ZM_SENDFILE_H
|
||||||
|
|
Loading…
Reference in New Issue