Commit | Line | Data |
---|---|---|
e6e3f12f | 1 | /* |
2f03f956 AT |
2 | Copyright (C) Andrew Tridgell 1996 |
3 | Copyright (C) Paul Mackerras 1996 | |
e6e3f12f | 4 | |
2f03f956 AT |
5 | This program is free software; you can redistribute it and/or modify |
6 | it under the terms of the GNU General Public License as published by | |
7 | the Free Software Foundation; either version 2 of the License, or | |
8 | (at your option) any later version. | |
e6e3f12f | 9 | |
2f03f956 AT |
10 | This program is distributed in the hope that it will be useful, |
11 | but WITHOUT ANY WARRANTY; without even the implied warranty of | |
12 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
13 | GNU General Public License for more details. | |
e6e3f12f | 14 | |
2f03f956 AT |
15 | You should have received a copy of the GNU General Public License |
16 | along with this program; if not, write to the Free Software | |
17 | Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. | |
18 | */ | |
19 | ||
20 | #include "rsync.h" | |
21 | ||
22 | extern int verbose; | |
2f03f956 AT |
23 | extern int csum_length; |
24 | extern struct stats stats; | |
25 | extern int io_error; | |
26 | extern int dry_run; | |
27 | extern int am_server; | |
28 | ||
29 | ||
0f9c48b1 MP |
30 | /** |
31 | * @file | |
32 | * | |
33 | * The sender gets checksums from the generator, calculates deltas, | |
34 | * and transmits them to the receiver. The sender process runs on the | |
35 | * machine holding the source files. | |
36 | **/ | |
37 | ||
38 | ||
fc0257c9 S |
39 | void read_sum_head(int f, struct sum_struct *sum) |
40 | { | |
d04e9c51 | 41 | extern int protocol_version; |
fc0257c9 S |
42 | |
43 | sum->count = read_int(f); | |
44 | sum->blength = read_int(f); | |
dfad66a8 | 45 | if (protocol_version < 27) { |
fc0257c9 | 46 | sum->s2length = csum_length; |
dfad66a8 | 47 | } else { |
e6e3f12f | 48 | sum->s2length = read_int(f); |
dfad66a8 | 49 | if (sum->s2length > MD4_SUM_LENGTH) { |
ef5075e0 WD |
50 | rprintf(FERROR, "Invalid checksum length %ld\n", |
51 | (long)sum->s2length); | |
dfad66a8 S |
52 | exit_cleanup(RERR_PROTOCOL); |
53 | } | |
54 | } | |
e6e3f12f | 55 | sum->remainder = read_int(f); |
fc0257c9 S |
56 | } |
57 | ||
ce8149b6 MP |
58 | /** |
59 | * Receive the checksums for a buffer | |
60 | **/ | |
2f03f956 AT |
61 | static struct sum_struct *receive_sums(int f) |
62 | { | |
63 | struct sum_struct *s; | |
64 | int i; | |
65 | OFF_T offset = 0; | |
66 | ||
58cadc86 | 67 | s = new(struct sum_struct); |
2f03f956 AT |
68 | if (!s) out_of_memory("receive_sums"); |
69 | ||
fc0257c9 S |
70 | read_sum_head(f, s); |
71 | ||
2f03f956 AT |
72 | s->sums = NULL; |
73 | ||
da9d12f5 WD |
74 | if (verbose > 3) { |
75 | rprintf(FINFO, "count=%ld n=%u rem=%u\n", | |
76 | (long)s->count, s->blength, s->remainder); | |
77 | } | |
2f03f956 | 78 | |
e6e3f12f | 79 | if (s->count == 0) |
2f03f956 AT |
80 | return(s); |
81 | ||
58cadc86 | 82 | s->sums = new_array(struct sum_buf, s->count); |
2f03f956 AT |
83 | if (!s->sums) out_of_memory("receive_sums"); |
84 | ||
e6e3f12f | 85 | for (i = 0; i < (int) s->count; i++) { |
2f03f956 | 86 | s->sums[i].sum1 = read_int(f); |
e6e3f12f | 87 | read_buf(f, s->sums[i].sum2, s->s2length); |
2f03f956 AT |
88 | |
89 | s->sums[i].offset = offset; | |
90 | s->sums[i].i = i; | |
91 | ||
a261989c | 92 | if (i == (int) s->count-1 && s->remainder != 0) { |
2f03f956 AT |
93 | s->sums[i].len = s->remainder; |
94 | } else { | |
fc0257c9 | 95 | s->sums[i].len = s->blength; |
2f03f956 AT |
96 | } |
97 | offset += s->sums[i].len; | |
98 | ||
99 | if (verbose > 3) | |
e6e3f12f S |
100 | rprintf(FINFO, "chunk[%d] len=%d offset=%.0f sum1=%08x\n", |
101 | i, s->sums[i].len, (double)s->sums[i].offset, s->sums[i].sum1); | |
2f03f956 AT |
102 | } |
103 | ||
104 | s->flength = offset; | |
105 | ||
106 | return s; | |
107 | } | |
108 | ||
109 | ||
110 | ||
e6e3f12f S |
111 | void send_files(struct file_list *flist, int f_out, int f_in) |
112 | { | |
c1659c79 | 113 | int fd = -1; |
2f03f956 | 114 | struct sum_struct *s; |
c1659c79 | 115 | struct map_struct *buf = NULL; |
2f03f956 | 116 | STRUCT_STAT st; |
e6e3f12f | 117 | char fname[MAXPATHLEN]; |
2f03f956 AT |
118 | int i; |
119 | struct file_struct *file; | |
120 | int phase = 0; | |
e6e3f12f | 121 | extern struct stats stats; |
1b7c47cb | 122 | struct stats initial_stats; |
64c3523a WD |
123 | extern int write_batch; |
124 | extern int read_batch; | |
125 | int checksums_match; | |
126 | int buff_len; | |
127 | char buff[CHUNK_SIZE]; | |
128 | int j; | |
129 | int done; | |
2f03f956 AT |
130 | |
131 | if (verbose > 2) | |
e6e3f12f | 132 | rprintf(FINFO, "send_files starting\n"); |
2f03f956 | 133 | |
2f03f956 | 134 | while (1) { |
e6e3f12f | 135 | int offset = 0; |
2f03f956 AT |
136 | |
137 | i = read_int(f_in); | |
138 | if (i == -1) { | |
e6e3f12f | 139 | if (phase == 0) { |
2f03f956 AT |
140 | phase++; |
141 | csum_length = SUM_LENGTH; | |
e6e3f12f | 142 | write_int(f_out, -1); |
2f03f956 | 143 | if (verbose > 2) |
e6e3f12f | 144 | rprintf(FINFO, "send_files phase=%d\n", phase); |
2f03f956 AT |
145 | continue; |
146 | } | |
147 | break; | |
148 | } | |
149 | ||
150 | if (i < 0 || i >= flist->count) { | |
e6e3f12f | 151 | rprintf(FERROR, "Invalid file index %d (count=%d)\n", |
2f03f956 | 152 | i, flist->count); |
65417579 | 153 | exit_cleanup(RERR_PROTOCOL); |
2f03f956 AT |
154 | } |
155 | ||
156 | file = flist->files[i]; | |
157 | ||
158 | stats.num_transferred_files++; | |
159 | stats.total_transferred_size += file->length; | |
160 | ||
161 | fname[0] = 0; | |
162 | if (file->basedir) { | |
e6e3f12f | 163 | strlcpy(fname, file->basedir, MAXPATHLEN); |
2f03f956 | 164 | if (strlen(fname) == MAXPATHLEN-1) { |
06c28400 | 165 | io_error |= IOERR_GENERAL; |
2f03f956 | 166 | rprintf(FERROR, "send_files failed on long-named directory %s\n", |
ea42541f | 167 | full_fname(fname)); |
2f03f956 AT |
168 | return; |
169 | } | |
e6e3f12f | 170 | strlcat(fname, "/", MAXPATHLEN); |
2f03f956 AT |
171 | offset = strlen(file->basedir)+1; |
172 | } | |
cc964a51 | 173 | f_name_to(file, fname + offset, MAXPATHLEN - offset); |
e6e3f12f S |
174 | |
175 | if (verbose > 2) | |
176 | rprintf(FINFO, "send_files(%d, %s)\n", i, fname); | |
177 | ||
178 | if (dry_run) { | |
42d4edc0 S |
179 | if (!am_server && verbose) { /* log transfer */ |
180 | rprintf(FINFO, "%s\n", fname+offset); | |
11a5a3c7 | 181 | } |
e6e3f12f | 182 | write_int(f_out, i); |
2f03f956 AT |
183 | continue; |
184 | } | |
185 | ||
1b7c47cb AT |
186 | initial_stats = stats; |
187 | ||
2f03f956 AT |
188 | s = receive_sums(f_in); |
189 | if (!s) { | |
06c28400 | 190 | io_error |= IOERR_GENERAL; |
e6e3f12f | 191 | rprintf(FERROR, "receive_sums failed\n"); |
2f03f956 AT |
192 | return; |
193 | } | |
76f79ba7 MP |
194 | |
195 | if (write_batch) | |
58422e83 | 196 | write_batch_csum_info(&i, s); |
e6e3f12f | 197 | |
6902ed17 | 198 | if (!read_batch) { |
c1659c79 MP |
199 | fd = do_open(fname, O_RDONLY, 0); |
200 | if (fd == -1) { | |
06c28400 WD |
201 | if (errno == ENOENT) { |
202 | io_error |= IOERR_VANISHED; | |
203 | rprintf(FINFO, "file has vanished: %s\n", | |
64c3523a | 204 | full_fname(fname)); |
06c28400 WD |
205 | } else { |
206 | io_error |= IOERR_GENERAL; | |
207 | rprintf(FERROR, "send_files failed to open %s: %s\n", | |
208 | full_fname(fname), strerror(errno)); | |
209 | } | |
c1659c79 MP |
210 | free_sums(s); |
211 | continue; | |
212 | } | |
e6e3f12f | 213 | |
c1659c79 | 214 | /* map the local file */ |
e6e3f12f | 215 | if (do_fstat(fd, &st) != 0) { |
06c28400 | 216 | io_error |= IOERR_GENERAL; |
ea42541f | 217 | rprintf(FERROR, "fstat failed: %s\n", strerror(errno)); |
c1659c79 MP |
218 | free_sums(s); |
219 | close(fd); | |
220 | return; | |
221 | } | |
e6e3f12f | 222 | |
c1659c79 | 223 | if (st.st_size > 0) { |
e6e3f12f | 224 | buf = map_file(fd, st.st_size); |
c1659c79 MP |
225 | } else { |
226 | buf = NULL; | |
227 | } | |
e6e3f12f | 228 | |
c1659c79 | 229 | if (verbose > 2) |
e6e3f12f S |
230 | rprintf(FINFO, "send_files mapped %s of size %.0f\n", |
231 | fname, (double)st.st_size); | |
232 | ||
233 | write_int(f_out, i); | |
11a5a3c7 | 234 | |
c1659c79 | 235 | if (write_batch) |
e6e3f12f | 236 | write_batch_delta_file((char *)&i, sizeof(i)); |
6902ed17 | 237 | |
fc0257c9 | 238 | write_sum_head(f_out, s); |
6902ed17 | 239 | } |
e6e3f12f S |
240 | |
241 | if (verbose > 2 && !read_batch) | |
242 | rprintf(FINFO, "calling match_sums %s\n", fname); | |
243 | ||
42d4edc0 S |
244 | if (!am_server && verbose) { /* log transfer */ |
245 | rprintf(FINFO, "%s\n", fname+offset); | |
11a5a3c7 | 246 | } |
83fff1aa AT |
247 | |
248 | set_compression(fname); | |
1b7c47cb | 249 | |
64c3523a | 250 | if (read_batch) { |
e6e3f12f S |
251 | /* read checksums originally computed on sender side */ |
252 | read_batch_csum_info(i, s, &checksums_match); | |
253 | if (checksums_match) { | |
cc964a51 | 254 | read_batch_delta_file((char*)&j, sizeof (int)); |
e6e3f12f S |
255 | if (j != i) { /* if flist index entries don't match*/ |
256 | rprintf(FINFO, "index mismatch in send_files\n"); | |
257 | rprintf(FINFO, "read index = %d flist ndx = %d\n", j, i); | |
258 | close_batch_delta_file(); | |
259 | close_batch_csums_file(); | |
260 | exit_cleanup(1); | |
261 | } else { | |
262 | write_int(f_out, j); | |
263 | write_sum_head(f_out, s); | |
264 | done = 0; | |
265 | while (!done) { | |
cc964a51 | 266 | read_batch_delta_file((char*)&buff_len, sizeof (int)); |
e6e3f12f S |
267 | write_int(f_out, buff_len); |
268 | if (buff_len == 0) { | |
269 | done = 1; | |
270 | } else { | |
271 | if (buff_len > 0) { | |
272 | read_batch_delta_file(buff, buff_len); | |
273 | write_buf(f_out, buff, buff_len); | |
274 | } | |
275 | } | |
276 | } /* end while */ | |
277 | read_batch_delta_file( buff, MD4_SUM_LENGTH); | |
278 | write_buf(f_out, buff, MD4_SUM_LENGTH); | |
279 | ||
280 | } /* j=i */ | |
281 | } else { /* not checksum match */ | |
282 | rprintf (FINFO, "readbatch & checksums don't match\n"); | |
283 | rprintf (FINFO, "filename=%s is being skipped\n", fname); | |
284 | continue; | |
285 | } | |
286 | } else { | |
287 | match_sums(f_out, s, buf, st.st_size); | |
288 | log_send(file, &initial_stats); | |
289 | } | |
6902ed17 | 290 | |
64c3523a | 291 | if (!read_batch) { |
6a7cc46c S |
292 | if (buf) { |
293 | j = unmap_file(buf); | |
294 | if (j) { | |
06c28400 | 295 | io_error |= IOERR_GENERAL; |
6a7cc46c S |
296 | rprintf(FERROR, |
297 | "read errors mapping %s: (%d) %s\n", | |
06c28400 | 298 | full_fname(fname), j, strerror(j)); |
6a7cc46c S |
299 | } |
300 | } | |
e6e3f12f | 301 | close(fd); |
6902ed17 | 302 | } |
e6e3f12f | 303 | |
2f03f956 | 304 | free_sums(s); |
e6e3f12f | 305 | |
2f03f956 | 306 | if (verbose > 2) |
e6e3f12f | 307 | rprintf(FINFO, "sender finished %s\n", fname); |
2f03f956 AT |
308 | } |
309 | ||
310 | if (verbose > 2) | |
e6e3f12f | 311 | rprintf(FINFO, "send files finished\n"); |
2f03f956 AT |
312 | |
313 | match_report(); | |
314 | ||
e6e3f12f | 315 | write_int(f_out, -1); |
64c3523a | 316 | if (write_batch || read_batch) { |
e6e3f12f S |
317 | close_batch_csums_file(); |
318 | close_batch_delta_file(); | |
6902ed17 MP |
319 | } |
320 | ||
2f03f956 AT |
321 | } |
322 | ||
323 | ||
324 | ||
325 | ||
326 |