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 | ||
208 | if (preserve_times && st->st_mtime != file->modtime) { | |
209 | updated = 1; | |
210 | if (set_modtime(fname,file->modtime) != 0) { | |
211 | fprintf(stderr,"failed to set times on %s : %s\n", | |
212 | fname,strerror(errno)); | |
213 | return; | |
214 | } | |
215 | } | |
216 | ||
217 | #ifdef HAVE_CHMOD | |
218 | if (preserve_perms && st->st_mode != file->mode) { | |
219 | updated = 1; | |
220 | if (chmod(fname,file->mode) != 0) { | |
221 | fprintf(stderr,"failed to set permissions on %s : %s\n", | |
222 | fname,strerror(errno)); | |
223 | return; | |
224 | } | |
225 | } | |
226 | #endif | |
227 | ||
228 | if ((preserve_uid && st->st_uid != file->uid) || | |
229 | (preserve_gid && st->st_gid != file->gid)) { | |
230 | updated = 1; | |
231 | if (chown(fname, | |
232 | preserve_uid?file->uid:-1, | |
233 | preserve_gid?file->gid:-1) != 0) { | |
234 | if (verbose>1 || preserve_uid) | |
235 | fprintf(stderr,"chown %s : %s\n",fname,strerror(errno)); | |
236 | return; | |
237 | } | |
238 | } | |
239 | ||
240 | if (verbose > 1 && report) { | |
241 | if (updated) | |
242 | fprintf(am_server?stderr:stdout,"%s\n",fname); | |
243 | else | |
244 | fprintf(am_server?stderr:stdout,"%s is uptodate\n",fname); | |
245 | } | |
246 | } | |
247 | ||
248 | ||
249 | void recv_generator(char *fname,struct file_list *flist,int i,int f_out) | |
250 | { | |
251 | int fd; | |
252 | struct stat st; | |
253 | char *buf; | |
254 | struct sum_struct *s; | |
255 | char sum[SUM_LENGTH]; | |
256 | int statret; | |
257 | ||
258 | if (verbose > 2) | |
259 | fprintf(stderr,"recv_generator(%s)\n",fname); | |
260 | ||
261 | statret = lstat(fname,&st); | |
262 | ||
263 | #if SUPPORT_LINKS | |
264 | if (preserve_links && S_ISLNK(flist->files[i].mode)) { | |
265 | char lnk[MAXPATHLEN]; | |
266 | int l; | |
267 | if (statret == 0) { | |
268 | l = readlink(fname,lnk,MAXPATHLEN-1); | |
269 | if (l > 0) { | |
270 | lnk[l] = 0; | |
271 | if (strcmp(lnk,flist->files[i].link) == 0) { | |
272 | if (verbose > 1) | |
273 | fprintf(am_server?stderr:stdout,"%s is uptodate\n",fname); | |
274 | return; | |
275 | } | |
276 | } | |
277 | } | |
278 | if (!dry_run) unlink(fname); | |
279 | if (!dry_run && symlink(flist->files[i].link,fname) != 0) { | |
280 | fprintf(stderr,"link %s -> %s : %s\n", | |
281 | fname,flist->files[i].link,strerror(errno)); | |
282 | } else { | |
283 | if (verbose) | |
284 | fprintf(am_server?stderr:stdout,"%s -> %s\n",fname,flist->files[i].link); | |
285 | } | |
286 | return; | |
287 | } | |
288 | #endif | |
289 | ||
290 | #ifdef HAVE_MKNOD | |
182dca5c | 291 | if (preserve_devices && IS_DEVICE(flist->files[i].mode)) { |
c627d613 AT |
292 | if (statret != 0 || |
293 | st.st_mode != flist->files[i].mode || | |
294 | st.st_rdev != flist->files[i].dev) { | |
295 | if (!dry_run) unlink(fname); | |
296 | if (verbose > 2) | |
297 | fprintf(stderr,"mknod(%s,0%o,0x%x)\n", | |
298 | fname,(int)flist->files[i].mode,(int)flist->files[i].dev); | |
299 | if (!dry_run && | |
300 | mknod(fname,flist->files[i].mode,flist->files[i].dev) != 0) { | |
301 | fprintf(stderr,"mknod %s : %s\n",fname,strerror(errno)); | |
302 | } else { | |
303 | set_perms(fname,&flist->files[i],NULL,0); | |
304 | if (verbose) | |
305 | fprintf(am_server?stderr:stdout,"%s\n",fname); | |
306 | } | |
307 | } else { | |
308 | set_perms(fname,&flist->files[i],&st,1); | |
309 | } | |
310 | return; | |
311 | } | |
312 | #endif | |
313 | ||
314 | if (!S_ISREG(flist->files[i].mode)) { | |
315 | fprintf(stderr,"skipping non-regular file %s\n",fname); | |
316 | return; | |
317 | } | |
318 | ||
319 | if (statret == -1) { | |
320 | if (errno == ENOENT) { | |
321 | write_int(f_out,i); | |
322 | if (!dry_run) send_sums(NULL,f_out); | |
323 | } else { | |
324 | if (verbose > 1) | |
325 | fprintf(stderr,"recv_generator failed to open %s\n",fname); | |
326 | } | |
327 | return; | |
328 | } | |
329 | ||
330 | if (!S_ISREG(st.st_mode)) { | |
331 | fprintf(stderr,"%s : not a regular file\n",fname); | |
332 | return; | |
333 | } | |
334 | ||
335 | if (update_only && st.st_mtime >= flist->files[i].modtime) { | |
336 | if (verbose > 1) | |
337 | fprintf(stderr,"%s is newer\n",fname); | |
338 | return; | |
339 | } | |
340 | ||
341 | if (always_checksum && S_ISREG(st.st_mode)) { | |
342 | file_checksum(fname,sum,st.st_size); | |
343 | } | |
344 | ||
345 | if (st.st_size == flist->files[i].length && | |
346 | ((!ignore_times && st.st_mtime == flist->files[i].modtime) || | |
347 | (always_checksum && S_ISREG(st.st_mode) && | |
43a481dc | 348 | memcmp(sum,flist->files[i].sum,csum_length) == 0))) { |
c627d613 AT |
349 | set_perms(fname,&flist->files[i],&st,1); |
350 | return; | |
351 | } | |
352 | ||
353 | if (dry_run) { | |
354 | write_int(f_out,i); | |
355 | return; | |
356 | } | |
357 | ||
358 | /* open the file */ | |
359 | fd = open(fname,O_RDONLY); | |
360 | ||
361 | if (fd == -1) { | |
362 | fprintf(stderr,"failed to open %s : %s\n",fname,strerror(errno)); | |
363 | return; | |
364 | } | |
365 | ||
366 | if (st.st_size > 0) { | |
367 | buf = map_file(fd,st.st_size); | |
c627d613 AT |
368 | } else { |
369 | buf = NULL; | |
370 | } | |
371 | ||
372 | if (verbose > 3) | |
373 | fprintf(stderr,"mapped %s of size %d\n",fname,(int)st.st_size); | |
374 | ||
375 | s = generate_sums(buf,st.st_size,block_size); | |
376 | ||
377 | write_int(f_out,i); | |
378 | send_sums(s,f_out); | |
379 | write_flush(f_out); | |
380 | ||
381 | close(fd); | |
382 | unmap_file(buf,st.st_size); | |
383 | ||
384 | free_sums(s); | |
385 | } | |
386 | ||
387 | ||
388 | ||
389 | static void receive_data(int f_in,char *buf,int fd,char *fname) | |
390 | { | |
391 | int i,n,remainder,len,count; | |
392 | off_t offset = 0; | |
393 | off_t offset2; | |
394 | ||
395 | count = read_int(f_in); | |
396 | n = read_int(f_in); | |
397 | remainder = read_int(f_in); | |
398 | ||
399 | for (i=read_int(f_in); i != 0; i=read_int(f_in)) { | |
400 | if (i > 0) { | |
401 | if (verbose > 3) | |
402 | fprintf(stderr,"data recv %d at %d\n",i,(int)offset); | |
403 | ||
404 | if (read_write(f_in,fd,i) != i) { | |
405 | fprintf(stderr,"write failed on %s : %s\n",fname,strerror(errno)); | |
406 | exit(1); | |
407 | } | |
408 | offset += i; | |
409 | } else { | |
410 | i = -(i+1); | |
411 | offset2 = i*n; | |
412 | len = n; | |
413 | if (i == count-1 && remainder != 0) | |
414 | len = remainder; | |
415 | ||
416 | if (verbose > 3) | |
417 | fprintf(stderr,"chunk[%d] of size %d at %d offset=%d\n", | |
418 | i,len,(int)offset2,(int)offset); | |
419 | ||
d9bea2dd | 420 | if (write(fd,map_ptr(buf,offset2,len),len) != len) { |
c627d613 AT |
421 | fprintf(stderr,"write failed on %s : %s\n",fname,strerror(errno)); |
422 | exit(1); | |
423 | } | |
424 | offset += len; | |
425 | } | |
426 | } | |
427 | } | |
428 | ||
429 | ||
430 | static void delete_one(struct file_struct *f) | |
431 | { | |
432 | if (!S_ISDIR(f->mode)) { | |
433 | if (!dry_run && unlink(f->name) != 0) { | |
434 | fprintf(stderr,"unlink %s : %s\n",f->name,strerror(errno)); | |
435 | } else if (verbose) { | |
436 | fprintf(stderr,"deleting %s\n",f->name); | |
437 | } | |
438 | } else { | |
439 | if (!dry_run && rmdir(f->name) != 0) { | |
440 | if (errno != ENOTEMPTY) | |
441 | fprintf(stderr,"rmdir %s : %s\n",f->name,strerror(errno)); | |
442 | } else if (verbose) { | |
443 | fprintf(stderr,"deleting directory %s\n",f->name); | |
444 | } | |
445 | } | |
446 | } | |
447 | ||
448 | ||
449 | static void delete_files(struct file_list *flist) | |
450 | { | |
451 | struct file_list *local_file_list; | |
452 | char *dot="."; | |
453 | int i; | |
454 | ||
4fe159a8 | 455 | if (cvs_exclude) |
79fbb6f5 | 456 | add_cvs_excludes(); |
4fe159a8 | 457 | |
c627d613 AT |
458 | if (!(local_file_list = send_file_list(-1,recurse,1,&dot))) |
459 | return; | |
460 | ||
461 | for (i=local_file_list->count;i>=0;i--) { | |
462 | if (!local_file_list->files[i].name) continue; | |
463 | if (-1 == flist_find(flist,&local_file_list->files[i])) { | |
464 | delete_one(&local_file_list->files[i]); | |
465 | } | |
466 | } | |
467 | } | |
468 | ||
469 | static char *cleanup_fname = NULL; | |
470 | ||
ac1eb754 | 471 | void sig_int(void) |
c627d613 AT |
472 | { |
473 | if (cleanup_fname) | |
474 | unlink(cleanup_fname); | |
475 | exit(1); | |
476 | } | |
477 | ||
478 | ||
479 | int recv_files(int f_in,struct file_list *flist,char *local_name) | |
480 | { | |
481 | int fd1,fd2; | |
482 | struct stat st; | |
483 | char *fname; | |
484 | char fnametmp[MAXPATHLEN]; | |
485 | char *buf; | |
486 | int i; | |
487 | ||
981791bd | 488 | if (verbose > 2) { |
c627d613 | 489 | fprintf(stderr,"recv_files(%d) starting\n",flist->count); |
981791bd | 490 | } |
c627d613 AT |
491 | |
492 | if (recurse && delete_mode && !local_name && flist->count>0) { | |
493 | delete_files(flist); | |
494 | } | |
495 | ||
496 | while (1) | |
497 | { | |
498 | i = read_int(f_in); | |
499 | if (i == -1) break; | |
500 | ||
501 | fname = flist->files[i].name; | |
502 | ||
503 | if (local_name) | |
504 | fname = local_name; | |
505 | ||
506 | if (dry_run) { | |
507 | if (!am_server && verbose) | |
508 | printf("%s\n",fname); | |
509 | continue; | |
510 | } | |
511 | ||
512 | if (verbose > 2) | |
513 | fprintf(stderr,"recv_files(%s)\n",fname); | |
514 | ||
515 | /* open the file */ | |
ac1eb754 | 516 | fd1 = open(fname,O_RDONLY); |
c627d613 | 517 | |
ac1eb754 | 518 | if (fd1 != -1 && fstat(fd1,&st) != 0) { |
c627d613 AT |
519 | fprintf(stderr,"fstat %s : %s\n",fname,strerror(errno)); |
520 | close(fd1); | |
521 | return -1; | |
522 | } | |
523 | ||
ac1eb754 | 524 | if (fd1 != -1 && !S_ISREG(st.st_mode)) { |
c627d613 AT |
525 | fprintf(stderr,"%s : not a regular file\n",fname); |
526 | close(fd1); | |
527 | return -1; | |
528 | } | |
529 | ||
ac1eb754 | 530 | if (fd1 != -1 && st.st_size > 0) { |
c627d613 | 531 | buf = map_file(fd1,st.st_size); |
c627d613 AT |
532 | } else { |
533 | buf = NULL; | |
534 | } | |
535 | ||
536 | if (verbose > 2) | |
537 | fprintf(stderr,"mapped %s of size %d\n",fname,(int)st.st_size); | |
538 | ||
539 | /* open tmp file */ | |
540 | sprintf(fnametmp,"%s.XXXXXX",fname); | |
541 | if (NULL == mktemp(fnametmp)) { | |
542 | fprintf(stderr,"mktemp %s failed\n",fnametmp); | |
543 | return -1; | |
544 | } | |
d9bea2dd | 545 | fd2 = open(fnametmp,O_WRONLY|O_CREAT,flist->files[i].mode); |
c627d613 AT |
546 | if (fd2 == -1) { |
547 | fprintf(stderr,"open %s : %s\n",fnametmp,strerror(errno)); | |
548 | return -1; | |
549 | } | |
550 | ||
551 | cleanup_fname = fnametmp; | |
552 | ||
553 | if (!am_server && verbose) | |
554 | printf("%s\n",fname); | |
555 | ||
556 | /* recv file data */ | |
557 | receive_data(f_in,buf,fd2,fname); | |
558 | ||
981791bd AT |
559 | if (fd1 != -1) { |
560 | unmap_file(buf,st.st_size); | |
561 | close(fd1); | |
562 | } | |
c627d613 AT |
563 | close(fd2); |
564 | ||
565 | if (verbose > 2) | |
566 | fprintf(stderr,"renaming %s to %s\n",fnametmp,fname); | |
567 | ||
568 | if (make_backups) { | |
569 | char fnamebak[MAXPATHLEN]; | |
570 | sprintf(fnamebak,"%s%s",fname,backup_suffix); | |
ac1eb754 | 571 | if (rename(fname,fnamebak) != 0 && errno != ENOENT) { |
c627d613 AT |
572 | fprintf(stderr,"rename %s %s : %s\n",fname,fnamebak,strerror(errno)); |
573 | exit(1); | |
574 | } | |
575 | } | |
576 | ||
577 | /* move tmp file over real file */ | |
578 | if (rename(fnametmp,fname) != 0) { | |
579 | fprintf(stderr,"rename %s -> %s : %s\n", | |
580 | fnametmp,fname,strerror(errno)); | |
581 | } | |
582 | ||
583 | cleanup_fname = NULL; | |
584 | ||
c627d613 AT |
585 | set_perms(fname,&flist->files[i],NULL,0); |
586 | } | |
587 | ||
588 | if (verbose > 2) | |
589 | fprintf(stderr,"recv_files finished\n"); | |
590 | ||
591 | return 0; | |
592 | } | |
593 | ||
594 | ||
595 | ||
596 | off_t send_files(struct file_list *flist,int f_out,int f_in) | |
597 | { | |
598 | int fd; | |
599 | struct sum_struct *s; | |
600 | char *buf; | |
601 | struct stat st; | |
602 | char fname[MAXPATHLEN]; | |
603 | off_t total=0; | |
604 | int i; | |
605 | ||
606 | if (verbose > 2) | |
607 | fprintf(stderr,"send_files starting\n"); | |
608 | ||
720b47f2 AT |
609 | setup_nonblocking(f_in,f_out); |
610 | ||
c627d613 AT |
611 | while (1) |
612 | { | |
613 | i = read_int(f_in); | |
614 | if (i == -1) break; | |
615 | ||
616 | fname[0] = 0; | |
617 | if (flist->files[i].dir) { | |
618 | strcpy(fname,flist->files[i].dir); | |
619 | strcat(fname,"/"); | |
620 | } | |
621 | strcat(fname,flist->files[i].name); | |
622 | ||
623 | if (verbose > 2) | |
624 | fprintf(stderr,"send_files(%d,%s)\n",i,fname); | |
625 | ||
626 | if (dry_run) { | |
627 | if (!am_server && verbose) | |
628 | printf("%s\n",fname); | |
629 | write_int(f_out,i); | |
630 | continue; | |
631 | } | |
632 | ||
633 | s = receive_sums(f_in); | |
634 | if (!s) { | |
635 | fprintf(stderr,"receive_sums failed\n"); | |
636 | return -1; | |
637 | } | |
638 | ||
639 | fd = open(fname,O_RDONLY); | |
640 | if (fd == -1) { | |
641 | fprintf(stderr,"send_files failed to open %s: %s\n", | |
642 | fname,strerror(errno)); | |
643 | continue; | |
644 | } | |
645 | ||
646 | /* map the local file */ | |
647 | if (fstat(fd,&st) != 0) { | |
648 | fprintf(stderr,"fstat failed : %s\n",strerror(errno)); | |
649 | return -1; | |
650 | } | |
651 | ||
652 | if (st.st_size > 0) { | |
653 | buf = map_file(fd,st.st_size); | |
c627d613 AT |
654 | } else { |
655 | buf = NULL; | |
656 | } | |
657 | ||
658 | if (verbose > 2) | |
659 | fprintf(stderr,"send_files mapped %s of size %d\n", | |
660 | fname,(int)st.st_size); | |
661 | ||
662 | write_int(f_out,i); | |
663 | ||
664 | write_int(f_out,s->count); | |
665 | write_int(f_out,s->n); | |
666 | write_int(f_out,s->remainder); | |
667 | ||
668 | if (verbose > 2) | |
669 | fprintf(stderr,"calling match_sums %s\n",fname); | |
670 | ||
671 | if (!am_server && verbose) | |
672 | printf("%s\n",fname); | |
673 | ||
720b47f2 | 674 | match_sums(f_out,s,buf,st.st_size); |
c627d613 AT |
675 | write_flush(f_out); |
676 | ||
677 | unmap_file(buf,st.st_size); | |
678 | close(fd); | |
679 | ||
680 | free_sums(s); | |
681 | ||
682 | if (verbose > 2) | |
683 | fprintf(stderr,"sender finished %s\n",fname); | |
684 | ||
685 | total += st.st_size; | |
686 | } | |
687 | ||
688 | match_report(); | |
689 | ||
690 | write_int(f_out,-1); | |
691 | write_flush(f_out); | |
692 | ||
693 | return total; | |
694 | } | |
695 | ||
696 | ||
697 | ||
698 | void generate_files(int f,struct file_list *flist,char *local_name) | |
699 | { | |
700 | int i; | |
701 | ||
702 | if (verbose > 2) | |
703 | fprintf(stderr,"generator starting pid=%d count=%d\n", | |
704 | (int)getpid(),flist->count); | |
705 | ||
706 | for (i = 0; i < flist->count; i++) { | |
707 | if (!flist->files[i].name) continue; | |
708 | if (S_ISDIR(flist->files[i].mode)) { | |
709 | if (dry_run) continue; | |
710 | if (mkdir(flist->files[i].name,flist->files[i].mode) != 0 && | |
711 | errno != EEXIST) { | |
712 | fprintf(stderr,"mkdir %s : %s\n", | |
713 | flist->files[i].name,strerror(errno)); | |
714 | } | |
715 | continue; | |
716 | } | |
717 | recv_generator(local_name?local_name:flist->files[i].name, | |
718 | flist,i,f); | |
719 | } | |
720 | write_int(f,-1); | |
721 | write_flush(f); | |
722 | if (verbose > 2) | |
723 | fprintf(stderr,"generator wrote %d\n",write_total()); | |
724 | } |