-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcopy.cpp
105 lines (84 loc) · 2.53 KB
/
copy.cpp
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
103
104
105
#include <fcntl.h>
#include <unistd.h>
#include <cerrno>
#include <cstddef>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#define ERROR(fmt, ...) \
do { \
fprintf(stderr, "%s:%d: " fmt "\n", __FILE__, __LINE__, \
##__VA_ARGS__); \
exit(1); \
} while (false)
// 128 MiB buffer
constexpr size_t BUF_SIZE = 128 * 1024;
char buf[BUF_SIZE] = {};
void copy_data(int src_fd, int dst_fd, size_t count) {
size_t total_copied = 0;
while (total_copied < count) {
size_t to_copy = count > BUF_SIZE ? BUF_SIZE : count;
ssize_t read_n = read(src_fd, buf, to_copy);
if (read_n < 0) {
ERROR("%s", strerror(errno));
}
ssize_t wrote_n = write(dst_fd, buf, read_n);
if (wrote_n < 0) {
ERROR("%s", strerror(errno));
}
if (size_t(read_n) < to_copy) {
break;
}
total_copied += read_n;
}
}
void copy_with_holes(int src_fd, int dst_fd) {
off_t end = lseek(src_fd, 0, SEEK_END);
if (end < 0) {
ERROR("%s", strerror(errno));
}
if (end == 0) {
return;
}
off_t next_data = 0;
while (true) {
off_t next_hole = lseek(src_fd, next_data, SEEK_HOLE);
if (next_hole < 0) {
ERROR("%s", strerror(errno));
}
if (lseek(src_fd, next_data, SEEK_SET) < 0) {
ERROR("%s", strerror(errno));
}
copy_data(src_fd, dst_fd, next_data - next_hole);
if (next_hole >= end) {
break;
}
next_data = lseek(src_fd, next_hole, SEEK_DATA);
if (next_data < 0) {
ERROR("%s", strerror(errno));
}
if (lseek(dst_fd, next_data, SEEK_SET) < 0) {
ERROR("%s", strerror(errno));
}
}
}
int main(int argc, char** argv) {
if (argc != 3) {
ERROR("Provide source and destionation files");
}
int src_fd = open(argv[1], O_RDONLY);
if (src_fd < 0) {
ERROR("%s: %s", argv[1], strerror(errno));
}
int dst_fd =
open(argv[2], O_RDWR | O_CREAT, S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH);
if (dst_fd < 0) {
ERROR("%s: %s", argv[2], strerror(errno));
}
if (ftruncate(dst_fd, 0) < 0) {
ERROR("%s", strerror(errno));
}
copy_with_holes(src_fd, dst_fd);
close(src_fd); // ignore errors
close(dst_fd);
}