zoneminder/src/zm_sendfile.h

49 lines
1.1 KiB
C
Raw Normal View History

#ifndef ZM_SENDFILE_H
#define ZM_SENDFILE_H
2015-02-24 14:20:55 +00:00
#ifdef HAVE_SENDFILE4_SUPPORT
#include <sys/sendfile.h>
#elif HAVE_SENDFILE7_SUPPORT
#include <sys/types.h>
#include <sys/socket.h>
#include <sys/uio.h>
#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, ssize_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
off_t sbytes = 0;
off_t ofs = offset ? *offset : 0;
ssize_t err = sendfile(in_fd, out_fd, ofs, size, nullptr, &sbytes, 0);
2017-06-22 13:55:45 +00:00
if (err && errno != EAGAIN)
return -errno;
return sbytes;
#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;
2017-06-22 13:55:45 +00:00
}
2015-02-24 14:20:55 +00:00
2022-12-02 21:43:45 +00:00
err = write(out_fd, buffer, size);
if (err < 0) {
Error("Unable to write %zu bytes: %s", size, strerror(errno));
return -errno;
}
return err;
2015-02-24 14:20:55 +00:00
#endif
}
2021-06-05 12:37:52 +00:00
#endif // ZM_SENDFILE_H