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