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