This code is based on splice-fromnet.c and splice-cp.c to splice from a socket to a pipe and from the pipe to a file but for some reason the first call to splice never returns.
static size_t splice_from_net_to_file(int infd, int outfd)
{
int p[2];
size_t total = 0;
if (pipe(p) == -1)
return error("pipe");
while (1) {
int ret;
ret = ssplice(infd, NULL, p[1], NULL, splice_size, 0);
if (ret < 0) {
close(p[0]);
close(p[1]);
return error("splice in pipe");
}
else if (!ret)
break;
while (ret > 0) {
int written = ssplice(p[0], NULL, outfd, NULL, ret, 0);
if (written <= 0) {
close(p[0]);
close(p[1]);
return error("splice out pipe");
}
ret -= written;
total += written;
}
}
close(p[0]);
close(p[1]);
return total;
}
I have tested this on linux 2.6.30.