Commit | Line | Data |
---|---|---|
c627d613 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 | ||
43a481dc AT |
22 | extern int csum_length; |
23 | ||
c627d613 AT |
24 | extern int verbose; |
25 | extern int am_server; | |
26 | extern int always_checksum; | |
27 | extern time_t starttime; | |
28 | ||
29 | extern char *backup_suffix; | |
30 | ||
31 | extern int block_size; | |
32 | extern int update_only; | |
33 | extern int make_backups; | |
34 | extern int preserve_links; | |
35 | extern int preserve_perms; | |
36 | extern int preserve_devices; | |
37 | extern int preserve_uid; | |
38 | extern int preserve_gid; | |
39 | extern int preserve_times; | |
40 | extern int dry_run; | |
41 | extern int ignore_times; | |
42 | extern int recurse; | |
43 | extern int delete_mode; | |
44 | extern int cvs_exclude; | |
45 | ||
46 | /* | |
47 | free a sums struct | |
48 | */ | |
49 | static void free_sums(struct sum_struct *s) | |
50 | { | |
51 | if (s->sums) free(s->sums); | |
52 | free(s); | |
53 | } | |
54 | ||
55 | ||
56 | ||
57 | /* | |
58 | send a sums struct down a fd | |
59 | */ | |
60 | static void send_sums(struct sum_struct *s,int f_out) | |
61 | { | |
62 | int i; | |
63 | ||
64 | /* tell the other guy how many we are going to be doing and how many | |
65 | bytes there are in the last chunk */ | |
66 | write_int(f_out,s?s->count:0); | |
67 | write_int(f_out,s?s->n:block_size); | |
68 | write_int(f_out,s?s->remainder:0); | |
69 | if (s) | |
70 | for (i=0;i<s->count;i++) { | |
71 | write_int(f_out,s->sums[i].sum1); | |
43a481dc | 72 | write_buf(f_out,s->sums[i].sum2,csum_length); |
c627d613 AT |
73 | } |
74 | write_flush(f_out); | |
75 | } | |
76 | ||
77 | ||
78 | /* | |
79 | generate a stream of signatures/checksums that describe a buffer | |
80 | ||
81 | generate approximately one checksum every n bytes | |
82 | */ | |
83 | static struct sum_struct *generate_sums(char *buf,off_t len,int n) | |
84 | { | |
85 | int i; | |
86 | struct sum_struct *s; | |
87 | int count; | |
88 | int block_len = n; | |
89 | int remainder = (len%block_len); | |
90 | off_t offset = 0; | |
91 | ||
92 | count = (len+(block_len-1))/block_len; | |
93 | ||
94 | s = (struct sum_struct *)malloc(sizeof(*s)); | |
95 | if (!s) out_of_memory("generate_sums"); | |
96 | ||
97 | s->count = count; | |
98 | s->remainder = remainder; | |
99 | s->n = n; | |
100 | s->flength = len; | |
101 | ||
102 | if (count==0) { | |
103 | s->sums = NULL; | |
104 | return s; | |
105 | } | |
106 | ||
107 | if (verbose > 3) | |
108 | fprintf(stderr,"count=%d rem=%d n=%d flength=%d\n", | |
109 | s->count,s->remainder,s->n,(int)s->flength); | |
110 | ||
111 | s->sums = (struct sum_buf *)malloc(sizeof(s->sums[0])*s->count); | |
112 | if (!s->sums) out_of_memory("generate_sums"); | |
113 | ||
114 | for (i=0;i<count;i++) { | |
115 | int n1 = MIN(len,n); | |
d9bea2dd | 116 | char *map = map_ptr(buf,offset,n1); |
c627d613 | 117 | |
d9bea2dd AT |
118 | s->sums[i].sum1 = get_checksum1(map,n1); |
119 | get_checksum2(map,n1,s->sums[i].sum2); | |
c627d613 AT |
120 | |
121 | s->sums[i].offset = offset; | |
122 | s->sums[i].len = n1; | |
123 | s->sums[i].i = i; | |
124 | ||
125 | if (verbose > 3) | |
126 | fprintf(stderr,"chunk[%d] offset=%d len=%d sum1=%08x\n", | |
127 | i,(int)s->sums[i].offset,s->sums[i].len,s->sums[i].sum1); | |
128 | ||
129 | len -= n1; | |
c627d613 AT |
130 | offset += n1; |
131 | } | |
132 | ||
133 | return s; | |
134 | } | |
135 | ||
136 | ||
137 | /* | |
138 | receive the checksums for a buffer | |
139 | */ | |
140 | static struct sum_struct *receive_sums(int f) | |
141 | { | |
142 | struct sum_struct *s; | |
143 | int i; | |
144 | off_t offset = 0; | |
145 | int block_len; | |
146 | ||
147 | s = (struct sum_struct *)malloc(sizeof(*s)); | |
148 | if (!s) out_of_memory("receive_sums"); | |
149 | ||
150 | s->count = read_int(f); | |
151 | s->n = read_int(f); | |
152 | s->remainder = read_int(f); | |
153 | s->sums = NULL; | |
154 | ||
155 | if (verbose > 3) | |
156 | fprintf(stderr,"count=%d n=%d rem=%d\n", | |
157 | s->count,s->n,s->remainder); | |
158 | ||
159 | block_len = s->n; | |
160 | ||
161 | if (s->count == 0) | |
162 | return(s); | |
163 | ||
164 | s->sums = (struct sum_buf *)malloc(sizeof(s->sums[0])*s->count); | |
165 | if (!s->sums) out_of_memory("receive_sums"); | |
166 | ||
167 | for (i=0;i<s->count;i++) { | |
168 | s->sums[i].sum1 = read_int(f); | |
43a481dc | 169 | read_buf(f,s->sums[i].sum2,csum_length); |
c627d613 AT |
170 | |
171 | s->sums[i].offset = offset; | |
172 | s->sums[i].i = i; | |
173 | ||
174 | if (i == s->count-1 && s->remainder != 0) { | |
175 | s->sums[i].len = s->remainder; | |
176 | } else { | |
177 | s->sums[i].len = s->n; | |
178 | } | |
179 | offset += s->sums[i].len; | |
180 | ||
181 | if (verbose > 3) | |
182 | fprintf(stderr,"chunk[%d] len=%d offset=%d sum1=%08x\n", | |
183 | i,s->sums[i].len,(int)s->sums[i].offset,s->sums[i].sum1); | |
184 | } | |
185 | ||
186 | s->flength = offset; | |
187 | ||
188 | return s; | |
189 | } | |
190 | ||
191 | ||
192 | static void set_perms(char *fname,struct file_struct *file,struct stat *st, | |
193 | int report) | |
194 | { | |
195 | int updated = 0; | |
196 | struct stat st2; | |
197 | ||
198 | if (dry_run) return; | |
199 | ||
200 | if (!st) { | |
201 | if (stat(fname,&st2) != 0) { | |
202 | fprintf(stderr,"stat %s : %s\n",fname,strerror(errno)); | |
203 | return; | |
204 | } | |
205 | st = &st2; | |
206 | } | |
207 | ||
7bec6a5c AT |
208 | if (preserve_times && !S_ISLNK(st->st_mode) && |
209 | st->st_mtime != file->modtime) { | |
c627d613 AT |
210 | updated = 1; |
211 | if (set_modtime(fname,file->modtime) != 0) { | |
212 | fprintf(stderr,"failed to set times on %s : %s\n", | |
213 | fname,strerror(errno)); | |
214 | return; | |
215 | } | |
216 | } | |
217 | ||
218 | #ifdef HAVE_CHMOD | |
7bec6a5c AT |
219 | if (preserve_perms && !S_ISLNK(st->st_mode) && |
220 | st->st_mode != file->mode) { | |
c627d613 AT |
221 | updated = 1; |
222 | if (chmod(fname,file->mode) != 0) { | |
223 | fprintf(stderr,"failed to set permissions on %s : %s\n", | |
224 | fname,strerror(errno)); | |
225 | return; | |
226 | } | |
227 | } | |
228 | #endif | |
229 | ||
230 | if ((preserve_uid && st->st_uid != file->uid) || | |
231 | (preserve_gid && st->st_gid != file->gid)) { | |
232 | updated = 1; | |
233 | if (chown(fname, | |
234 | preserve_uid?file->uid:-1, | |
235 | preserve_gid?file->gid:-1) != 0) { | |
236 | if (verbose>1 || preserve_uid) | |
237 | fprintf(stderr,"chown %s : %s\n",fname,strerror(errno)); | |
238 | return; | |
239 | } | |
240 | } | |
241 | ||
242 | if (verbose > 1 && report) { | |
243 | if (updated) | |
244 | fprintf(am_server?stderr:stdout,"%s\n",fname); | |
245 | else | |
246 | fprintf(am_server?stderr:stdout,"%s is uptodate\n",fname); | |
247 | } | |
248 | } | |
249 | ||
250 | ||
251 | void recv_generator(char *fname,struct file_list *flist,int i,int f_out) | |
252 | { | |
253 | int fd; | |
254 | struct stat st; | |
255 | char *buf; | |
256 | struct sum_struct *s; | |
257 | char sum[SUM_LENGTH]; | |
258 | int statret; | |
259 | ||
260 | if (verbose > 2) | |
261 | fprintf(stderr,"recv_generator(%s)\n",fname); | |
262 | ||
263 | statret = lstat(fname,&st); | |
264 | ||
265 | #if SUPPORT_LINKS | |
266 | if (preserve_links && S_ISLNK(flist->files[i].mode)) { | |
267 | char lnk[MAXPATHLEN]; | |
268 | int l; | |
269 | if (statret == 0) { | |
270 | l = readlink(fname,lnk,MAXPATHLEN-1); | |
271 | if (l > 0) { | |
272 | lnk[l] = 0; | |
273 | if (strcmp(lnk,flist->files[i].link) == 0) { | |
7bec6a5c | 274 | set_perms(fname,&flist->files[i],&st,1); |
c627d613 AT |
275 | return; |
276 | } | |
277 | } | |
278 | } | |
279 | if (!dry_run) unlink(fname); | |
280 | if (!dry_run && symlink(flist->files[i].link,fname) != 0) { | |
281 | fprintf(stderr,"link %s -> %s : %s\n", | |
282 | fname,flist->files[i].link,strerror(errno)); | |
283 | } else { | |
7bec6a5c | 284 | set_perms(fname,&flist->files[i],NULL,0); |
c627d613 | 285 | if (verbose) |
7bec6a5c AT |
286 | fprintf(am_server?stderr:stdout,"%s -> %s\n", |
287 | fname,flist->files[i].link); | |
c627d613 AT |
288 | } |
289 | return; | |
290 | } | |
291 | #endif | |
292 | ||
293 | #ifdef HAVE_MKNOD | |
182dca5c | 294 | if (preserve_devices && IS_DEVICE(flist->files[i].mode)) { |
c627d613 AT |
295 | if (statret != 0 || |
296 | st.st_mode != flist->files[i].mode || | |
297 | st.st_rdev != flist->files[i].dev) { | |
298 | if (!dry_run) unlink(fname); | |
299 | if (verbose > 2) | |
300 | fprintf(stderr,"mknod(%s,0%o,0x%x)\n", | |
301 | fname,(int)flist->files[i].mode,(int)flist->files[i].dev); | |
302 | if (!dry_run && | |
303 | mknod(fname,flist->files[i].mode,flist->files[i].dev) != 0) { | |
304 | fprintf(stderr,"mknod %s : %s\n",fname,strerror(errno)); | |
305 | } else { | |
306 | set_perms(fname,&flist->files[i],NULL,0); | |
307 | if (verbose) | |
308 | fprintf(am_server?stderr:stdout,"%s\n",fname); | |
309 | } | |
310 | } else { | |
311 | set_perms(fname,&flist->files[i],&st,1); | |
312 | } | |
313 | return; | |
314 | } | |
315 | #endif | |
316 | ||
317 | if (!S_ISREG(flist->files[i].mode)) { | |
318 | fprintf(stderr,"skipping non-regular file %s\n",fname); | |
319 | return; | |
320 | } | |
321 | ||
322 | if (statret == -1) { | |
323 | if (errno == ENOENT) { | |
324 | write_int(f_out,i); | |
325 | if (!dry_run) send_sums(NULL,f_out); | |
326 | } else { | |
327 | if (verbose > 1) | |
328 | fprintf(stderr,"recv_generator failed to open %s\n",fname); | |
329 | } | |
330 | return; | |
331 | } | |
332 | ||
333 | if (!S_ISREG(st.st_mode)) { | |
334 | fprintf(stderr,"%s : not a regular file\n",fname); | |
335 | return; | |
336 | } | |
337 | ||
338 | if (update_only && st.st_mtime >= flist->files[i].modtime) { | |
339 | if (verbose > 1) | |
340 | fprintf(stderr,"%s is newer\n",fname); | |
341 | return; | |
342 | } | |
343 | ||
344 | if (always_checksum && S_ISREG(st.st_mode)) { | |
345 | file_checksum(fname,sum,st.st_size); | |
346 | } | |
347 | ||
348 | if (st.st_size == flist->files[i].length && | |
349 | ((!ignore_times && st.st_mtime == flist->files[i].modtime) || | |
350 | (always_checksum && S_ISREG(st.st_mode) && | |
43a481dc | 351 | memcmp(sum,flist->files[i].sum,csum_length) == 0))) { |
c627d613 AT |
352 | set_perms(fname,&flist->files[i],&st,1); |
353 | return; | |
354 | } | |
355 | ||
356 | if (dry_run) { | |
357 | write_int(f_out,i); | |
358 | return; | |
359 | } | |
360 | ||
361 | /* open the file */ | |
362 | fd = open(fname,O_RDONLY); | |
363 | ||
364 | if (fd == -1) { | |
365 | fprintf(stderr,"failed to open %s : %s\n",fname,strerror(errno)); | |
366 | return; | |
367 | } | |
368 | ||
369 | if (st.st_size > 0) { | |
370 | buf = map_file(fd,st.st_size); | |
c627d613 AT |
371 | } else { |
372 | buf = NULL; | |
373 | } | |
374 | ||
375 | if (verbose > 3) | |
376 | fprintf(stderr,"mapped %s of size %d\n",fname,(int)st.st_size); | |
377 | ||
378 | s = generate_sums(buf,st.st_size,block_size); | |
379 | ||
380 | write_int(f_out,i); | |
381 | send_sums(s,f_out); | |
382 | write_flush(f_out); | |
383 | ||
384 | close(fd); | |
385 | unmap_file(buf,st.st_size); | |
386 | ||
387 | free_sums(s); | |
388 | } | |
389 | ||
390 | ||
391 | ||
392 | static void receive_data(int f_in,char *buf,int fd,char *fname) | |
393 | { | |
394 | int i,n,remainder,len,count; | |
395 | off_t offset = 0; | |
396 | off_t offset2; | |
397 | ||
398 | count = read_int(f_in); | |
399 | n = read_int(f_in); | |
400 | remainder = read_int(f_in); | |
401 | ||
402 | for (i=read_int(f_in); i != 0; i=read_int(f_in)) { | |
403 | if (i > 0) { | |
404 | if (verbose > 3) | |
405 | fprintf(stderr,"data recv %d at %d\n",i,(int)offset); | |
406 | ||
407 | if (read_write(f_in,fd,i) != i) { | |
408 | fprintf(stderr,"write failed on %s : %s\n",fname,strerror(errno)); | |
409 | exit(1); | |
410 | } | |
411 | offset += i; | |
412 | } else { | |
413 | i = -(i+1); | |
414 | offset2 = i*n; | |
415 | len = n; | |
416 | if (i == count-1 && remainder != 0) | |
417 | len = remainder; | |
418 | ||
419 | if (verbose > 3) | |
420 | fprintf(stderr,"chunk[%d] of size %d at %d offset=%d\n", | |
421 | i,len,(int)offset2,(int)offset); | |
422 | ||
7bec6a5c | 423 | if (write_sparse(fd,map_ptr(buf,offset2,len),len) != len) { |
c627d613 AT |
424 | fprintf(stderr,"write failed on %s : %s\n",fname,strerror(errno)); |
425 | exit(1); | |
426 | } | |
427 | offset += len; | |
428 | } | |
429 | } | |
7bec6a5c AT |
430 | |
431 | if (offset > 0 && sparse_end(fd) != 0) { | |
432 | fprintf(stderr,"write failed on %s : %s\n",fname,strerror(errno)); | |
433 | exit(1); | |
434 | } | |
c627d613 AT |
435 | } |
436 | ||
437 | ||
438 | static void delete_one(struct file_struct *f) | |
439 | { | |
440 | if (!S_ISDIR(f->mode)) { | |
441 | if (!dry_run && unlink(f->name) != 0) { | |
442 | fprintf(stderr,"unlink %s : %s\n",f->name,strerror(errno)); | |
443 | } else if (verbose) { | |
444 | fprintf(stderr,"deleting %s\n",f->name); | |
445 | } | |
446 | } else { | |
447 | if (!dry_run && rmdir(f->name) != 0) { | |
448 | if (errno != ENOTEMPTY) | |
449 | fprintf(stderr,"rmdir %s : %s\n",f->name,strerror(errno)); | |
450 | } else if (verbose) { | |
451 | fprintf(stderr,"deleting directory %s\n",f->name); | |
452 | } | |
453 | } | |
454 | } | |
455 | ||
456 | ||
457 | static void delete_files(struct file_list *flist) | |
458 | { | |
459 | struct file_list *local_file_list; | |
460 | char *dot="."; | |
461 | int i; | |
462 | ||
4fe159a8 | 463 | if (cvs_exclude) |
79fbb6f5 | 464 | add_cvs_excludes(); |
4fe159a8 | 465 | |
c627d613 AT |
466 | if (!(local_file_list = send_file_list(-1,recurse,1,&dot))) |
467 | return; | |
468 | ||
469 | for (i=local_file_list->count;i>=0;i--) { | |
470 | if (!local_file_list->files[i].name) continue; | |
471 | if (-1 == flist_find(flist,&local_file_list->files[i])) { | |
472 | delete_one(&local_file_list->files[i]); | |
473 | } | |
474 | } | |
475 | } | |
476 | ||
477 | static char *cleanup_fname = NULL; | |
478 | ||
ac1eb754 | 479 | void sig_int(void) |
c627d613 AT |
480 | { |
481 | if (cleanup_fname) | |
482 | unlink(cleanup_fname); | |
483 | exit(1); | |
484 | } | |
485 | ||
486 | ||
487 | int recv_files(int f_in,struct file_list *flist,char *local_name) | |
488 | { | |
489 | int fd1,fd2; | |
490 | struct stat st; | |
491 | char *fname; | |
492 | char fnametmp[MAXPATHLEN]; | |
493 | char *buf; | |
494 | int i; | |
495 | ||
981791bd | 496 | if (verbose > 2) { |
c627d613 | 497 | fprintf(stderr,"recv_files(%d) starting\n",flist->count); |
981791bd | 498 | } |
c627d613 AT |
499 | |
500 | if (recurse && delete_mode && !local_name && flist->count>0) { | |
501 | delete_files(flist); | |
502 | } | |
503 | ||
504 | while (1) | |
505 | { | |
506 | i = read_int(f_in); | |
507 | if (i == -1) break; | |
508 | ||
509 | fname = flist->files[i].name; | |
510 | ||
511 | if (local_name) | |
512 | fname = local_name; | |
513 | ||
514 | if (dry_run) { | |
515 | if (!am_server && verbose) | |
516 | printf("%s\n",fname); | |
517 | continue; | |
518 | } | |
519 | ||
520 | if (verbose > 2) | |
521 | fprintf(stderr,"recv_files(%s)\n",fname); | |
522 | ||
523 | /* open the file */ | |
ac1eb754 | 524 | fd1 = open(fname,O_RDONLY); |
c627d613 | 525 | |
ac1eb754 | 526 | if (fd1 != -1 && fstat(fd1,&st) != 0) { |
c627d613 AT |
527 | fprintf(stderr,"fstat %s : %s\n",fname,strerror(errno)); |
528 | close(fd1); | |
529 | return -1; | |
530 | } | |
531 | ||
ac1eb754 | 532 | if (fd1 != -1 && !S_ISREG(st.st_mode)) { |
c627d613 AT |
533 | fprintf(stderr,"%s : not a regular file\n",fname); |
534 | close(fd1); | |
535 | return -1; | |
536 | } | |
537 | ||
ac1eb754 | 538 | if (fd1 != -1 && st.st_size > 0) { |
c627d613 | 539 | buf = map_file(fd1,st.st_size); |
c627d613 AT |
540 | } else { |
541 | buf = NULL; | |
542 | } | |
543 | ||
544 | if (verbose > 2) | |
545 | fprintf(stderr,"mapped %s of size %d\n",fname,(int)st.st_size); | |
546 | ||
547 | /* open tmp file */ | |
548 | sprintf(fnametmp,"%s.XXXXXX",fname); | |
549 | if (NULL == mktemp(fnametmp)) { | |
550 | fprintf(stderr,"mktemp %s failed\n",fnametmp); | |
551 | return -1; | |
552 | } | |
d9bea2dd | 553 | fd2 = open(fnametmp,O_WRONLY|O_CREAT,flist->files[i].mode); |
c627d613 AT |
554 | if (fd2 == -1) { |
555 | fprintf(stderr,"open %s : %s\n",fnametmp,strerror(errno)); | |
556 | return -1; | |
557 | } | |
558 | ||
559 | cleanup_fname = fnametmp; | |
560 | ||
561 | if (!am_server && verbose) | |
562 | printf("%s\n",fname); | |
563 | ||
564 | /* recv file data */ | |
565 | receive_data(f_in,buf,fd2,fname); | |
566 | ||
981791bd AT |
567 | if (fd1 != -1) { |
568 | unmap_file(buf,st.st_size); | |
569 | close(fd1); | |
570 | } | |
c627d613 AT |
571 | close(fd2); |
572 | ||
573 | if (verbose > 2) | |
574 | fprintf(stderr,"renaming %s to %s\n",fnametmp,fname); | |
575 | ||
576 | if (make_backups) { | |
577 | char fnamebak[MAXPATHLEN]; | |
578 | sprintf(fnamebak,"%s%s",fname,backup_suffix); | |
ac1eb754 | 579 | if (rename(fname,fnamebak) != 0 && errno != ENOENT) { |
c627d613 AT |
580 | fprintf(stderr,"rename %s %s : %s\n",fname,fnamebak,strerror(errno)); |
581 | exit(1); | |
582 | } | |
583 | } | |
584 | ||
585 | /* move tmp file over real file */ | |
586 | if (rename(fnametmp,fname) != 0) { | |
587 | fprintf(stderr,"rename %s -> %s : %s\n", | |
588 | fnametmp,fname,strerror(errno)); | |
589 | } | |
590 | ||
591 | cleanup_fname = NULL; | |
592 | ||
c627d613 AT |
593 | set_perms(fname,&flist->files[i],NULL,0); |
594 | } | |
595 | ||
596 | if (verbose > 2) | |
597 | fprintf(stderr,"recv_files finished\n"); | |
598 | ||
599 | return 0; | |
600 | } | |
601 | ||
602 | ||
603 | ||
604 | off_t send_files(struct file_list *flist,int f_out,int f_in) | |
605 | { | |
606 | int fd; | |
607 | struct sum_struct *s; | |
608 | char *buf; | |
609 | struct stat st; | |
610 | char fname[MAXPATHLEN]; | |
611 | off_t total=0; | |
612 | int i; | |
613 | ||
614 | if (verbose > 2) | |
615 | fprintf(stderr,"send_files starting\n"); | |
616 | ||
720b47f2 AT |
617 | setup_nonblocking(f_in,f_out); |
618 | ||
c627d613 AT |
619 | while (1) |
620 | { | |
621 | i = read_int(f_in); | |
622 | if (i == -1) break; | |
623 | ||
624 | fname[0] = 0; | |
625 | if (flist->files[i].dir) { | |
626 | strcpy(fname,flist->files[i].dir); | |
627 | strcat(fname,"/"); | |
628 | } | |
629 | strcat(fname,flist->files[i].name); | |
630 | ||
631 | if (verbose > 2) | |
632 | fprintf(stderr,"send_files(%d,%s)\n",i,fname); | |
633 | ||
634 | if (dry_run) { | |
635 | if (!am_server && verbose) | |
636 | printf("%s\n",fname); | |
637 | write_int(f_out,i); | |
638 | continue; | |
639 | } | |
640 | ||
641 | s = receive_sums(f_in); | |
642 | if (!s) { | |
643 | fprintf(stderr,"receive_sums failed\n"); | |
644 | return -1; | |
645 | } | |
646 | ||
647 | fd = open(fname,O_RDONLY); | |
648 | if (fd == -1) { | |
649 | fprintf(stderr,"send_files failed to open %s: %s\n", | |
650 | fname,strerror(errno)); | |
651 | continue; | |
652 | } | |
653 | ||
654 | /* map the local file */ | |
655 | if (fstat(fd,&st) != 0) { | |
656 | fprintf(stderr,"fstat failed : %s\n",strerror(errno)); | |
657 | return -1; | |
658 | } | |
659 | ||
660 | if (st.st_size > 0) { | |
661 | buf = map_file(fd,st.st_size); | |
c627d613 AT |
662 | } else { |
663 | buf = NULL; | |
664 | } | |
665 | ||
666 | if (verbose > 2) | |
667 | fprintf(stderr,"send_files mapped %s of size %d\n", | |
668 | fname,(int)st.st_size); | |
669 | ||
670 | write_int(f_out,i); | |
671 | ||
672 | write_int(f_out,s->count); | |
673 | write_int(f_out,s->n); | |
674 | write_int(f_out,s->remainder); | |
675 | ||
676 | if (verbose > 2) | |
677 | fprintf(stderr,"calling match_sums %s\n",fname); | |
678 | ||
679 | if (!am_server && verbose) | |
680 | printf("%s\n",fname); | |
681 | ||
720b47f2 | 682 | match_sums(f_out,s,buf,st.st_size); |
c627d613 AT |
683 | write_flush(f_out); |
684 | ||
685 | unmap_file(buf,st.st_size); | |
686 | close(fd); | |
687 | ||
688 | free_sums(s); | |
689 | ||
690 | if (verbose > 2) | |
691 | fprintf(stderr,"sender finished %s\n",fname); | |
692 | ||
693 | total += st.st_size; | |
694 | } | |
695 | ||
696 | match_report(); | |
697 | ||
698 | write_int(f_out,-1); | |
699 | write_flush(f_out); | |
700 | ||
701 | return total; | |
702 | } | |
703 | ||
704 | ||
705 | ||
706 | void generate_files(int f,struct file_list *flist,char *local_name) | |
707 | { | |
708 | int i; | |
709 | ||
710 | if (verbose > 2) | |
711 | fprintf(stderr,"generator starting pid=%d count=%d\n", | |
712 | (int)getpid(),flist->count); | |
713 | ||
714 | for (i = 0; i < flist->count; i++) { | |
715 | if (!flist->files[i].name) continue; | |
716 | if (S_ISDIR(flist->files[i].mode)) { | |
717 | if (dry_run) continue; | |
718 | if (mkdir(flist->files[i].name,flist->files[i].mode) != 0 && | |
719 | errno != EEXIST) { | |
720 | fprintf(stderr,"mkdir %s : %s\n", | |
721 | flist->files[i].name,strerror(errno)); | |
722 | } | |
723 | continue; | |
724 | } | |
725 | recv_generator(local_name?local_name:flist->files[i].name, | |
726 | flist,i,f); | |
727 | } | |
728 | write_int(f,-1); | |
729 | write_flush(f); | |
730 | if (verbose > 2) | |
731 | fprintf(stderr,"generator wrote %d\n",write_total()); | |
732 | } |