-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcp.c
178 lines (148 loc) · 3.2 KB
/
cp.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
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
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
#include <stdio.h>
#include <stddef.h>
#include <stdlib.h>
#include <fcntl.h>
#include <unistd.h>
#include <sys/stat.h>
#include <sys/mman.h>
#include <sys/param.h>
#include <string.h>
#include "md5.h"
int
copy_file(char *src, char *dst)
{
int from_fd;
int to_fd;
char *p;
char *q;
struct stat src_sb, dst_sb;
int rc;
size_t it;
size_t bytes;
char *src_md5;
char *dst_md5;
from_fd = open(src, O_RDONLY);
if (from_fd == -1) {
printf("Err open: %s\n", src);
return (-1);
}
to_fd = open(dst, O_RDWR | O_CREAT | O_EXCL, 0600);
if (to_fd == -1) {
printf("Err creat %s for %s\n", dst, src);
close (from_fd);
return (-1);
}
rc = fstat(from_fd, &src_sb);
if (rc == -1) {
printf("Err stat %s\n", src);
return (-1);
}
rc = fstat(to_fd, &dst_sb);
if (rc == -1) {
printf("Err stat %s\n", dst);
return (-1);
}
if (!(src_sb.st_mode & S_IFREG)) {
printf("Err stat S_IFREG %s\n", src);
return (-1);
}
p = mmap(0, src_sb.st_size, PROT_READ, MAP_FILE | MAP_SHARED, from_fd, 0);
if (p == MAP_FAILED) {
printf("mmap failed %s\n", src);
return (-1);
}
posix_fallocate(to_fd, 0, src_sb.st_size);
q = mmap(0, src_sb.st_size, PROT_READ|PROT_WRITE, MAP_FILE | MAP_SHARED, to_fd, 0);
if (q == MAP_FAILED) {
printf("mmap failed %s\n", dst);
return (-1);
}
bcopy(p, q, src_sb.st_size);
src_md5 = md5sumbuf(p, src_sb.st_size);
munmap(p, src_sb.st_size);
munmap(q, src_sb.st_size);
close(from_fd);
close(to_fd);
dst_md5 = md5sum(dst);
if (strcmp(src_md5, dst_md5) != 0) {
printf("Copy hash err: got %s expected %s\n", dst_md5, src_md5);
free(src_md5);
free(dst_md5);
return (-1);
}
free(src_md5);
free(dst_md5);
return (0);
}
int
copy_file_hash(char *src, char *dst, char *srchash)
{
int from_fd;
int to_fd;
char *p;
char *q;
struct stat src_sb, dst_sb;
int rc;
size_t it;
size_t bytes;
char *src_md5 = srchash;
char *dst_md5;
from_fd = open(src, O_RDONLY);
if (from_fd == -1) {
printf("Err open: %s\n", src);
return (-1);
}
to_fd = open(dst, O_RDWR | O_CREAT | O_EXCL, 0600);
if (to_fd == -1) {
printf("Err creat %s for %s\n", dst, src);
close (from_fd);
return (-1);
}
rc = fstat(from_fd, &src_sb);
if (rc == -1) {
printf("Err stat %s\n", src);
return (-1);
}
rc = fstat(to_fd, &dst_sb);
if (rc == -1) {
printf("Err stat %s\n", dst);
return (-1);
}
if (!(src_sb.st_mode & S_IFREG)) {
printf("Err stat S_IFREG %s\n", src);
return (-1);
}
p = mmap(0, src_sb.st_size, PROT_READ, MAP_FILE | MAP_SHARED | MAP_POPULATE, from_fd, 0);
if (p == MAP_FAILED) {
printf("mmap failed %s\n", src);
return (-1);
}
posix_fallocate(to_fd, 0, src_sb.st_size);
q = mmap(0, src_sb.st_size, PROT_READ | PROT_WRITE, MAP_FILE | MAP_SHARED, to_fd, 0);
if (q == MAP_FAILED) {
printf("mmap failed %s\n", dst);
return (-1);
}
bcopy(p, q, src_sb.st_size);
munmap(p, src_sb.st_size);
munmap(q, src_sb.st_size);
close(from_fd);
fsync(to_fd);
close(to_fd);
dst_md5 = md5sum(dst);
if (strcmp(src_md5, dst_md5) != 0) {
printf("Copy hash err: got %s expected %s\n", dst_md5, src_md5);
free(src_md5);
free(dst_md5);
return (-1);
}
free(dst_md5);
return (0);
}
#ifdef TEST
main(argc, argv)
char *argv[];
{
copy_file(argv[1], argv[2]);
}
#endif