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