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