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