You are right about limiting sendfile for this. And yes, splice can help, but itβs not trivial: splice requires that at least one of the descriptors of the source or target file be a channel. Thus, you cannot directly splice from a socket into a simple file descriptor.
Conceptually, what you can do to make it work:
- configure your inbound fd socket and your fd output file as usual.
- create channel with
pipe(2) - in the loop:
- read from the socket to the write side of the handset using
splice - write on the read side of the channel to the file using
splice also
Repeat the last steps until all data has been read.
Zero-Copy on Linux with sendfile () and splice () has an implementation of this technique.
source share