1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
|
/*
* Copyright 2005-2021 Gentoo Foundation
* Distributed under the terms of the GNU General Public License v2
*
* Copyright 2011-2016 Mike Frysinger - <vapier@gentoo.org>
* Copyright 2021- Fabian Groffen - <grobian@gentoo.org>
*/
#include "main.h"
#include "safe_io.h"
#include "copy_file.h"
/* includes for when sendfile is available */
#include <fcntl.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <unistd.h>
#if defined(HAVE_SENDFILE4_SUPPORT)
/* Linux/Solaris */
# include <sys/sendfile.h>
#elif defined(HAVE_SENDFILE6_SUPPORT) || defined(HAVE_SENDFILE7_SUPPORT)
/* macOS (since Darwin 9) + FreeBSD */
# include <sys/socket.h>
# include <sys/uio.h>
#endif
int copy_file_fd(int fd_src, int fd_dst)
{
#if defined(HAVE_SENDFILE4_SUPPORT) || \
defined(HAVE_SENDFILE6_SUPPORT) || \
defined(HAVE_SENDFILE7_SUPPORT)
struct stat stat_buf;
ssize_t ret;
size_t len;
off_t offset = 0;
if (fstat(fd_src, &stat_buf) != -1) {
len = (size_t)stat_buf.st_size;
#if defined(HAVE_SENDFILE4_SUPPORT)
/* Linux/Solaris */
ret = sendfile(fd_dst, fd_src, &offset, len);
/* everything looks fine, return success */
if (ret == (ssize_t)len)
return 0;
#elif defined(HAVE_SENDFILE6_SUPPORT)
/* macOS (since Darwin 9) */
offset = len;
ret = (ssize_t)sendfile(fd_src, fd_dst, 0, &offset, NULL, 0);
/* everything looks fine, return success */
if (offset == (off_t)len)
return 0;
#elif defined(HAVE_SENDFILE7_SUPPORT)
/* FreeBSD */
ret = (ssize_t)sendfile(fd_src, fd_dst, offset, len, NULL, &offset, 0);
/* everything looks fine, return success */
if (offset == (off_t)len)
return 0;
#endif
/* fall back to read/write, rewind the fd */
lseek(fd_src, 0, SEEK_SET);
}
#endif /* HAVE_SENDFILE */
/* fallback, keep in its own scope, so we avoid 64K stack alloc if
* sendfile works properly */
{
ssize_t rcnt, wcnt;
char buf[64 * 1024];
while (1) {
rcnt = safe_read(fd_src, buf, sizeof(buf));
if (rcnt < 0)
return -1;
else if (rcnt == 0)
return 0;
wcnt = safe_write(fd_dst, buf, rcnt);
if (wcnt == -1)
return -1;
}
}
}
int copy_file(FILE *src, FILE *dst)
{
ssize_t rcnt, wcnt;
char buf[64 * 1024];
while (1) {
rcnt = fread(buf, 1, sizeof(buf), src);
if (rcnt < 0)
return -1;
else if (rcnt == 0)
return 0;
wcnt = fwrite(buf, 1, rcnt, dst);
if (wcnt == -1 || wcnt != rcnt)
return -1;
}
}
|