ul_copy_file: use sendfile

Suggested-by: Karel Zak <kzak@redhat.com>
Reviewed-by: Sami Kerola <kerolasa@iki.fi>
Signed-off-by: Egor Chelak <egor.chelak@gmail.com>
This commit is contained in:
Egor Chelak 2020-11-06 11:15:11 +02:00
parent b9dcd38462
commit a8b4e7cad1
1 changed files with 28 additions and 2 deletions

View File

@ -6,10 +6,12 @@
*/
#include <stdio.h>
#include <stdlib.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <unistd.h>
#include <sys/time.h>
#include <sys/resource.h>
#include <sys/sendfile.h>
#include <string.h>
#include "c.h"
@ -248,8 +250,7 @@ char *stripoff_last_component(char *path)
return p + 1;
}
/* Copies the contents of a file. Returns -1 on read error, -2 on write error. */
int ul_copy_file(int from, int to)
static int copy_file_simple(int from, int to)
{
ssize_t nr, nw, off;
char buf[8 * 1024];
@ -265,3 +266,28 @@ int ul_copy_file(int from, int to)
#endif
return 0;
}
/* Copies the contents of a file. Returns -1 on read error, -2 on write error. */
int ul_copy_file(int from, int to)
{
struct stat st;
ssize_t nw;
off_t left;
if (fstat(from, &st) == -1)
return -1;
if (!S_ISREG(st.st_mode))
return copy_file_simple(from, to);
for (left = st.st_size; left != 0; left -= nw) {
if ((nw = sendfile(to, from, NULL, left)) < 0)
return copy_file_simple(from, to);
if (!nw)
return 0;
}
/* For extra robustness, treat st_size as advisory and ensure that we
* actually get EOF. */
while ((nw = sendfile(to, from, NULL, 1024*1024)) != 0)
if (nw < 0)
return copy_file_simple(from, to);
return 0;
}