-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsendfile.c
70 lines (56 loc) · 1.64 KB
/
sendfile.c
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
#include <fcntl.h> /* open */
#include <stdio.h> /* perror */
#include <stdlib.h> /* EXIT_*, size_t, ssize_t */
#include <unistd.h> /* read, write, close */
#include <stdbool.h> /* true, false */
#include <sys/stat.h> /* struct stat */
#include <sys/sendfile.h> /* sendfile */
#include "mycp.h"
/* man 2 sendfile:
*
* sendfile() will transfer at most 0x7ffff000 (2,147,479,552) bytes,
* returning the number of bytes actually transferred. (This is true on
* both 32-bit and 64-bit systems.)
*/
#define MAX_SENDFILE_TRANSFER 2147479552
bool sendfile_cp(const char *from_filename, const char *to_filename) {
struct stat status;
int from_fd, to_fd;
off_t transfer_size, filesize, offset = 0;
bool response = false;
from_fd = open(from_filename, O_RDONLY);
if(-1==from_fd) {
perror("from file");
goto finish;
}
to_fd = open(to_filename, O_RDWR|O_CREAT|O_TRUNC, S_IWUSR|S_IRUSR);
if(-1==to_fd) {
perror("to file");
goto close_from;
}
if(-1==fstat(from_fd, &status)) {
perror("fstat");
goto close_all;
}
filesize = status.st_size;
transfer_size = (filesize>MAX_SENDFILE_TRANSFER ? MAX_SENDFILE_TRANSFER : filesize);
while(offset<filesize) {
/* no check errors: try transfer data until finished. */
sendfile(to_fd, from_fd, &offset, transfer_size);
}
if(-1==fsync(from_fd)) {
perror("fsync()");
goto close_all;
}
response = true;
close_all:
if(-1==close(to_fd)) {
perror("to_fd");
}
close_from:
if(-1==close(from_fd)) {
perror("from_fd");
}
finish:
return(response);
}