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