*** empty log message ***
[rsync/rsync.git] / rsync.c
CommitLineData
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
22extern int csum_length;
23
c627d613
AT
24extern int verbose;
25extern int am_server;
26extern int always_checksum;
27extern time_t starttime;
28
c6e7fcb4
AT
29extern int remote_version;
30
c627d613 31extern char *backup_suffix;
950ab32d 32extern char *tmpdir;
c627d613 33
82306bf6 34extern int whole_file;
c627d613
AT
35extern int block_size;
36extern int update_only;
37extern int make_backups;
38extern int preserve_links;
dc5ddbcc 39extern int preserve_hard_links;
c627d613
AT
40extern int preserve_perms;
41extern int preserve_devices;
42extern int preserve_uid;
43extern int preserve_gid;
44extern int preserve_times;
45extern int dry_run;
46extern int ignore_times;
47extern int recurse;
48extern int delete_mode;
49extern int cvs_exclude;
7b8356d0 50extern int am_root;
6574b4f7 51extern int relative_paths;
c627d613
AT
52
53/*
54 free a sums struct
55 */
56static void free_sums(struct sum_struct *s)
57{
58 if (s->sums) free(s->sums);
59 free(s);
60}
61
62
63
64/*
65 send a sums struct down a fd
66 */
67static void send_sums(struct sum_struct *s,int f_out)
68{
69 int i;
70
71 /* tell the other guy how many we are going to be doing and how many
72 bytes there are in the last chunk */
73 write_int(f_out,s?s->count:0);
74 write_int(f_out,s?s->n:block_size);
75 write_int(f_out,s?s->remainder:0);
76 if (s)
77 for (i=0;i<s->count;i++) {
78 write_int(f_out,s->sums[i].sum1);
43a481dc 79 write_buf(f_out,s->sums[i].sum2,csum_length);
c627d613
AT
80 }
81 write_flush(f_out);
82}
83
84
85/*
86 generate a stream of signatures/checksums that describe a buffer
87
88 generate approximately one checksum every n bytes
89 */
c6e7fcb4 90static struct sum_struct *generate_sums(struct map_struct *buf,off_t len,int n)
c627d613
AT
91{
92 int i;
93 struct sum_struct *s;
94 int count;
95 int block_len = n;
96 int remainder = (len%block_len);
97 off_t offset = 0;
98
99 count = (len+(block_len-1))/block_len;
100
101 s = (struct sum_struct *)malloc(sizeof(*s));
102 if (!s) out_of_memory("generate_sums");
103
104 s->count = count;
105 s->remainder = remainder;
106 s->n = n;
107 s->flength = len;
108
109 if (count==0) {
110 s->sums = NULL;
111 return s;
112 }
113
114 if (verbose > 3)
dc5ddbcc 115 fprintf(FERROR,"count=%d rem=%d n=%d flength=%d\n",
c627d613
AT
116 s->count,s->remainder,s->n,(int)s->flength);
117
118 s->sums = (struct sum_buf *)malloc(sizeof(s->sums[0])*s->count);
119 if (!s->sums) out_of_memory("generate_sums");
120
121 for (i=0;i<count;i++) {
122 int n1 = MIN(len,n);
d9bea2dd 123 char *map = map_ptr(buf,offset,n1);
c627d613 124
d9bea2dd
AT
125 s->sums[i].sum1 = get_checksum1(map,n1);
126 get_checksum2(map,n1,s->sums[i].sum2);
c627d613
AT
127
128 s->sums[i].offset = offset;
129 s->sums[i].len = n1;
130 s->sums[i].i = i;
131
132 if (verbose > 3)
dc5ddbcc 133 fprintf(FERROR,"chunk[%d] offset=%d len=%d sum1=%08x\n",
c627d613
AT
134 i,(int)s->sums[i].offset,s->sums[i].len,s->sums[i].sum1);
135
136 len -= n1;
c627d613
AT
137 offset += n1;
138 }
139
140 return s;
141}
142
143
144/*
145 receive the checksums for a buffer
146 */
147static struct sum_struct *receive_sums(int f)
148{
149 struct sum_struct *s;
150 int i;
151 off_t offset = 0;
c627d613
AT
152
153 s = (struct sum_struct *)malloc(sizeof(*s));
154 if (!s) out_of_memory("receive_sums");
155
156 s->count = read_int(f);
157 s->n = read_int(f);
158 s->remainder = read_int(f);
159 s->sums = NULL;
160
161 if (verbose > 3)
dc5ddbcc 162 fprintf(FERROR,"count=%d n=%d rem=%d\n",
c627d613
AT
163 s->count,s->n,s->remainder);
164
c627d613
AT
165 if (s->count == 0)
166 return(s);
167
168 s->sums = (struct sum_buf *)malloc(sizeof(s->sums[0])*s->count);
169 if (!s->sums) out_of_memory("receive_sums");
170
171 for (i=0;i<s->count;i++) {
172 s->sums[i].sum1 = read_int(f);
43a481dc 173 read_buf(f,s->sums[i].sum2,csum_length);
c627d613
AT
174
175 s->sums[i].offset = offset;
176 s->sums[i].i = i;
177
178 if (i == s->count-1 && s->remainder != 0) {
179 s->sums[i].len = s->remainder;
180 } else {
181 s->sums[i].len = s->n;
182 }
183 offset += s->sums[i].len;
184
185 if (verbose > 3)
dc5ddbcc 186 fprintf(FERROR,"chunk[%d] len=%d offset=%d sum1=%08x\n",
c627d613
AT
187 i,s->sums[i].len,(int)s->sums[i].offset,s->sums[i].sum1);
188 }
189
190 s->flength = offset;
191
192 return s;
193}
194
195
7b8356d0
AT
196static int set_perms(char *fname,struct file_struct *file,struct stat *st,
197 int report)
c627d613
AT
198{
199 int updated = 0;
200 struct stat st2;
201
7b8356d0 202 if (dry_run) return 0;
c627d613
AT
203
204 if (!st) {
82306bf6 205 if (link_stat(fname,&st2) != 0) {
dc5ddbcc 206 fprintf(FERROR,"stat %s : %s\n",fname,strerror(errno));
7b8356d0 207 return 0;
c627d613
AT
208 }
209 st = &st2;
210 }
211
7bec6a5c
AT
212 if (preserve_times && !S_ISLNK(st->st_mode) &&
213 st->st_mtime != file->modtime) {
c627d613
AT
214 updated = 1;
215 if (set_modtime(fname,file->modtime) != 0) {
dc5ddbcc 216 fprintf(FERROR,"failed to set times on %s : %s\n",
c627d613 217 fname,strerror(errno));
7b8356d0 218 return 0;
c627d613
AT
219 }
220 }
221
222#ifdef HAVE_CHMOD
7bec6a5c
AT
223 if (preserve_perms && !S_ISLNK(st->st_mode) &&
224 st->st_mode != file->mode) {
c627d613
AT
225 updated = 1;
226 if (chmod(fname,file->mode) != 0) {
dc5ddbcc 227 fprintf(FERROR,"failed to set permissions on %s : %s\n",
c627d613 228 fname,strerror(errno));
7b8356d0 229 return 0;
c627d613
AT
230 }
231 }
232#endif
233
7b8356d0 234 if ((am_root && preserve_uid && st->st_uid != file->uid) ||
c627d613
AT
235 (preserve_gid && st->st_gid != file->gid)) {
236 updated = 1;
8bf73749
AT
237 if (lchown(fname,
238 (am_root&&preserve_uid)?file->uid:-1,
239 preserve_gid?file->gid:-1) != 0) {
c627d613 240 if (verbose>1 || preserve_uid)
dc5ddbcc 241 fprintf(FERROR,"chown %s : %s\n",fname,strerror(errno));
7b8356d0 242 return updated;
c627d613
AT
243 }
244 }
245
246 if (verbose > 1 && report) {
247 if (updated)
dc5ddbcc 248 fprintf(FINFO,"%s\n",fname);
c627d613 249 else
dc5ddbcc 250 fprintf(FINFO,"%s is uptodate\n",fname);
c627d613 251 }
7b8356d0 252 return updated;
c627d613
AT
253}
254
255
964ca2ec
AT
256/* choose whether to skip a particular file */
257static int skip_file(char *fname,
258 struct file_struct *file, struct stat *st)
259{
260 if (st->st_size != file->length) {
261 return 0;
262 }
263
264 /* if always checksum is set then we use the checksum instead
265 of the file time to determine whether to sync */
266 if (always_checksum && S_ISREG(st->st_mode)) {
267 char sum[MD4_SUM_LENGTH];
268 file_checksum(fname,sum,st->st_size);
269 return (memcmp(sum,file->sum,csum_length) == 0);
270 }
271
272 if (ignore_times) {
273 return 0;
274 }
275
276 return (st->st_mtime == file->modtime);
277}
278
279
c627d613
AT
280void recv_generator(char *fname,struct file_list *flist,int i,int f_out)
281{
282 int fd;
283 struct stat st;
c6e7fcb4 284 struct map_struct *buf;
c627d613 285 struct sum_struct *s;
c627d613 286 int statret;
dc5ddbcc 287 struct file_struct *file = &flist->files[i];
c627d613
AT
288
289 if (verbose > 2)
9e31c482 290 fprintf(FERROR,"recv_generator(%s,%d)\n",fname,i);
c627d613 291
82306bf6 292 statret = link_stat(fname,&st);
c627d613 293
7b8356d0
AT
294 if (S_ISDIR(file->mode)) {
295 if (dry_run) return;
296 if (statret == 0 && !S_ISDIR(st.st_mode)) {
297 if (unlink(fname) != 0) {
298 fprintf(FERROR,"unlink %s : %s\n",fname,strerror(errno));
299 return;
300 }
301 statret = -1;
302 }
6574b4f7
AT
303 if (statret != 0 && mkdir(fname,file->mode) != 0 && errno != EEXIST) {
304 if (!(relative_paths && errno==ENOENT &&
305 create_directory_path(fname)==0 &&
306 mkdir(fname,file->mode)==0)) {
307 fprintf(FERROR,"mkdir %s : %s (2)\n",
308 fname,strerror(errno));
309 }
310 }
7b8356d0
AT
311 if (set_perms(fname,file,NULL,0) && verbose)
312 fprintf(FINFO,"%s/\n",fname);
313 return;
314 }
315
dc5ddbcc 316 if (preserve_links && S_ISLNK(file->mode)) {
cbbe4892 317#if SUPPORT_LINKS
c627d613
AT
318 char lnk[MAXPATHLEN];
319 int l;
320 if (statret == 0) {
321 l = readlink(fname,lnk,MAXPATHLEN-1);
322 if (l > 0) {
323 lnk[l] = 0;
dc5ddbcc
AT
324 if (strcmp(lnk,file->link) == 0) {
325 set_perms(fname,file,&st,1);
c627d613
AT
326 return;
327 }
328 }
329 }
330 if (!dry_run) unlink(fname);
dc5ddbcc
AT
331 if (!dry_run && symlink(file->link,fname) != 0) {
332 fprintf(FERROR,"link %s -> %s : %s\n",
333 fname,file->link,strerror(errno));
c627d613 334 } else {
dc5ddbcc 335 set_perms(fname,file,NULL,0);
c627d613 336 if (verbose)
dc5ddbcc
AT
337 fprintf(FINFO,"%s -> %s\n",
338 fname,file->link);
c627d613 339 }
cbbe4892 340#endif
c627d613
AT
341 return;
342 }
c627d613
AT
343
344#ifdef HAVE_MKNOD
7b8356d0 345 if (am_root && preserve_devices && IS_DEVICE(file->mode)) {
c627d613 346 if (statret != 0 ||
dc5ddbcc
AT
347 st.st_mode != file->mode ||
348 st.st_rdev != file->rdev) {
c627d613
AT
349 if (!dry_run) unlink(fname);
350 if (verbose > 2)
dc5ddbcc
AT
351 fprintf(FERROR,"mknod(%s,0%o,0x%x)\n",
352 fname,(int)file->mode,(int)file->rdev);
c627d613 353 if (!dry_run &&
dc5ddbcc
AT
354 mknod(fname,file->mode,file->rdev) != 0) {
355 fprintf(FERROR,"mknod %s : %s\n",fname,strerror(errno));
c627d613 356 } else {
dc5ddbcc 357 set_perms(fname,file,NULL,0);
c627d613 358 if (verbose)
dc5ddbcc 359 fprintf(FINFO,"%s\n",fname);
c627d613
AT
360 }
361 } else {
dc5ddbcc 362 set_perms(fname,file,&st,1);
c627d613
AT
363 }
364 return;
365 }
366#endif
367
dc5ddbcc
AT
368 if (preserve_hard_links && check_hard_link(file)) {
369 if (verbose > 1)
370 fprintf(FINFO,"%s is a hard link\n",file->name);
371 return;
372 }
373
374 if (!S_ISREG(file->mode)) {
375 fprintf(FERROR,"skipping non-regular file %s\n",fname);
c627d613
AT
376 return;
377 }
378
379 if (statret == -1) {
380 if (errno == ENOENT) {
381 write_int(f_out,i);
382 if (!dry_run) send_sums(NULL,f_out);
383 } else {
384 if (verbose > 1)
dc5ddbcc 385 fprintf(FERROR,"recv_generator failed to open %s\n",fname);
c627d613
AT
386 }
387 return;
388 }
389
390 if (!S_ISREG(st.st_mode)) {
7b8356d0
AT
391 /* its not a regular file on the receiving end, but it is on the
392 sending end. If its a directory then skip it (too dangerous to
393 do a recursive deletion??) otherwise try to unlink it */
394 if (S_ISDIR(st.st_mode)) {
395 fprintf(FERROR,"ERROR: %s is a directory\n",fname);
396 return;
397 }
398 if (unlink(fname) != 0) {
399 fprintf(FERROR,"%s : not a regular file (generator)\n",fname);
400 return;
401 }
402
403 /* now pretend the file didn't exist */
404 write_int(f_out,i);
405 if (!dry_run) send_sums(NULL,f_out);
c627d613
AT
406 return;
407 }
408
dc5ddbcc 409 if (update_only && st.st_mtime >= file->modtime) {
c627d613 410 if (verbose > 1)
dc5ddbcc 411 fprintf(FERROR,"%s is newer\n",fname);
c627d613
AT
412 return;
413 }
414
964ca2ec 415 if (skip_file(fname, file, &st)) {
dc5ddbcc 416 set_perms(fname,file,&st,1);
c627d613
AT
417 return;
418 }
419
420 if (dry_run) {
421 write_int(f_out,i);
422 return;
423 }
424
82306bf6
AT
425 if (whole_file) {
426 write_int(f_out,i);
427 send_sums(NULL,f_out);
428 return;
429 }
430
c627d613
AT
431 /* open the file */
432 fd = open(fname,O_RDONLY);
433
434 if (fd == -1) {
dc5ddbcc 435 fprintf(FERROR,"failed to open %s : %s\n",fname,strerror(errno));
f3644f1f 436 fprintf(FERROR,"skipping %s\n",fname);
c627d613
AT
437 return;
438 }
439
440 if (st.st_size > 0) {
441 buf = map_file(fd,st.st_size);
c627d613
AT
442 } else {
443 buf = NULL;
444 }
445
446 if (verbose > 3)
9e31c482 447 fprintf(FERROR,"gen mapped %s of size %d\n",fname,(int)st.st_size);
c627d613
AT
448
449 s = generate_sums(buf,st.st_size,block_size);
450
9e31c482
AT
451 if (verbose > 2)
452 fprintf(FERROR,"sending sums for %d\n",i);
453
c627d613
AT
454 write_int(f_out,i);
455 send_sums(s,f_out);
456 write_flush(f_out);
457
458 close(fd);
1cdc8b50 459 if (buf) unmap_file(buf);
c627d613
AT
460
461 free_sums(s);
462}
463
464
465
9e31c482 466static int receive_data(int f_in,struct map_struct *buf,int fd,char *fname)
c627d613
AT
467{
468 int i,n,remainder,len,count;
469 off_t offset = 0;
470 off_t offset2;
70d794dc 471 char *data;
ebb0a6f6
AT
472 static char file_sum1[MD4_SUM_LENGTH];
473 static char file_sum2[MD4_SUM_LENGTH];
9e31c482 474 char *map=NULL;
c627d613
AT
475
476 count = read_int(f_in);
477 n = read_int(f_in);
478 remainder = read_int(f_in);
479
9e31c482
AT
480 sum_init();
481
70d794dc 482 for (i=recv_token(f_in,&data); i != 0; i=recv_token(f_in,&data)) {
c627d613
AT
483 if (i > 0) {
484 if (verbose > 3)
dc5ddbcc 485 fprintf(FERROR,"data recv %d at %d\n",i,(int)offset);
c627d613 486
9e31c482
AT
487 sum_update(data,i);
488
7b8356d0 489 if (fd != -1 && write_sparse(fd,data,i) != i) {
dc5ddbcc 490 fprintf(FERROR,"write failed on %s : %s\n",fname,strerror(errno));
34ccb63e 491 exit_cleanup(1);
c627d613
AT
492 }
493 offset += i;
494 } else {
495 i = -(i+1);
496 offset2 = i*n;
497 len = n;
498 if (i == count-1 && remainder != 0)
499 len = remainder;
500
501 if (verbose > 3)
dc5ddbcc 502 fprintf(FERROR,"chunk[%d] of size %d at %d offset=%d\n",
c627d613
AT
503 i,len,(int)offset2,(int)offset);
504
9e31c482
AT
505 map = map_ptr(buf,offset2,len);
506
861c20b4 507 see_token(map, len);
9e31c482
AT
508 sum_update(map,len);
509
7b8356d0 510 if (fd != -1 && write_sparse(fd,map,len) != len) {
dc5ddbcc 511 fprintf(FERROR,"write failed on %s : %s\n",fname,strerror(errno));
34ccb63e 512 exit_cleanup(1);
c627d613
AT
513 }
514 offset += len;
515 }
516 }
7bec6a5c 517
7b8356d0 518 if (fd != -1 && offset > 0 && sparse_end(fd) != 0) {
dc5ddbcc 519 fprintf(FERROR,"write failed on %s : %s\n",fname,strerror(errno));
34ccb63e 520 exit_cleanup(1);
7bec6a5c 521 }
9e31c482
AT
522
523 sum_end(file_sum1);
524
525 if (remote_version >= 14) {
ebb0a6f6 526 read_buf(f_in,file_sum2,MD4_SUM_LENGTH);
9e31c482
AT
527 if (verbose > 2)
528 fprintf(FERROR,"got file_sum\n");
7b8356d0 529 if (fd != -1 && memcmp(file_sum1,file_sum2,MD4_SUM_LENGTH) != 0)
9e31c482
AT
530 return 0;
531 }
532 return 1;
c627d613
AT
533}
534
535
536static void delete_one(struct file_struct *f)
537{
538 if (!S_ISDIR(f->mode)) {
539 if (!dry_run && unlink(f->name) != 0) {
dc5ddbcc 540 fprintf(FERROR,"unlink %s : %s\n",f->name,strerror(errno));
c627d613 541 } else if (verbose) {
dc5ddbcc 542 fprintf(FERROR,"deleting %s\n",f->name);
c627d613
AT
543 }
544 } else {
545 if (!dry_run && rmdir(f->name) != 0) {
546 if (errno != ENOTEMPTY)
dc5ddbcc 547 fprintf(FERROR,"rmdir %s : %s\n",f->name,strerror(errno));
c627d613 548 } else if (verbose) {
dc5ddbcc 549 fprintf(FERROR,"deleting directory %s\n",f->name);
c627d613
AT
550 }
551 }
552}
553
554
3b3a2fbc
AT
555/* yuck! This function wouldn't have been necessary if I had the sorting
556 algorithm right. Unfortunately fixing the sorting algorithm would introduce
557 a backward incompatibility as file list indexes are sent over the link.
558
559 The aim is to see if a directory has already had the deletion algorithm applied
560 to it (due to recursion), and if so to skip it. The bisection is to
561 prevent this being an n^2 algorithm */
562static int delete_already_done(struct file_list *flist,int j)
563{
564 int low=0,high=j-1;
565 char *name;
566 char *p;
567
568 if (j == 0) return 0;
569
570 name = strdup(flist->files[j].name);
571
572 if (!name) {
573 fprintf(FERROR,"out of memory in delete_already_done");
574 exit_cleanup(1);
575 }
576
577 p = strrchr(name,'/');
578 if (!p) {
579 free(name);
580 return 0;
581 }
582 *p = 0;
583
584 while (low != high) {
585 int mid = (low+high)/2;
586 int ret = strcmp(flist->files[flist_up(flist, mid)].name,name);
587 if (ret == 0) {
588 free(name);
589 return 1;
590 }
591 if (ret > 0) {
592 high=mid;
593 } else {
594 low=mid+1;
595 }
596 }
597
598 low = flist_up(flist, low);
599
600 if (strcmp(flist->files[low].name,name) == 0) {
601 free(name);
602 return 1;
603 }
604
605 free(name);
606 return 0;
607}
608
609
e92338c8
AT
610/* this deletes any files on the receiving side that are not present
611 on the sending side. For version 1.6.4 I have changed the behaviour
612 to match more closely what most people seem to expect of this option */
c627d613
AT
613static void delete_files(struct file_list *flist)
614{
615 struct file_list *local_file_list;
e92338c8 616 int i, j;
c627d613 617
4fe159a8 618 if (cvs_exclude)
79fbb6f5 619 add_cvs_excludes();
4fe159a8 620
e92338c8 621 for (j=0;j<flist->count;j++) {
3b3a2fbc
AT
622 char *name = flist->files[j].name;
623
e92338c8 624 if (!S_ISDIR(flist->files[j].mode)) continue;
3b3a2fbc
AT
625
626 if (delete_already_done(flist, j)) continue;
627
628 if (!(local_file_list = send_file_list(-1,1,&name)))
d966ee25 629 continue;
3b3a2fbc 630
e92338c8 631 if (verbose > 1)
3b3a2fbc 632 fprintf(FINFO,"deleting in %s\n", name);
e92338c8
AT
633
634 for (i=local_file_list->count-1;i>=0;i--) {
635 if (!local_file_list->files[i].name) continue;
636 if (-1 == flist_find(flist,&local_file_list->files[i])) {
637 delete_one(&local_file_list->files[i]);
638 }
639 }
c627d613
AT
640 }
641}
642
643static char *cleanup_fname = NULL;
644
34ccb63e 645void exit_cleanup(int code)
c627d613 646{
82306bf6
AT
647 if (cleanup_fname)
648 unlink(cleanup_fname);
649 signal(SIGUSR1, SIG_IGN);
650 if (code) {
651#ifdef GETPGRP_VOID
652 kill(-getpgrp(), SIGUSR1);
653#else
654 kill(-getpgrp(getpid()), SIGUSR1);
655#endif
656 }
657 exit(code);
34ccb63e
AT
658}
659
660void sig_int(void)
661{
662 exit_cleanup(1);
c627d613
AT
663}
664
665
c6e7fcb4 666int recv_files(int f_in,struct file_list *flist,char *local_name,int f_gen)
c627d613
AT
667{
668 int fd1,fd2;
669 struct stat st;
670 char *fname;
671 char fnametmp[MAXPATHLEN];
c6e7fcb4 672 struct map_struct *buf;
c627d613 673 int i;
dc5ddbcc 674 struct file_struct *file;
c6e7fcb4 675 int phase=0;
9e31c482 676 int recv_ok;
c627d613 677
981791bd 678 if (verbose > 2) {
dc5ddbcc 679 fprintf(FERROR,"recv_files(%d) starting\n",flist->count);
981791bd 680 }
c627d613
AT
681
682 if (recurse && delete_mode && !local_name && flist->count>0) {
683 delete_files(flist);
684 }
685
686 while (1)
9e31c482 687 {
c627d613 688 i = read_int(f_in);
c6e7fcb4
AT
689 if (i == -1) {
690 if (phase==0 && remote_version >= 13) {
691 phase++;
9e31c482
AT
692 csum_length = SUM_LENGTH;
693 if (verbose > 2)
694 fprintf(FERROR,"recv_files phase=%d\n",phase);
c6e7fcb4
AT
695 write_int(f_gen,-1);
696 write_flush(f_gen);
697 continue;
698 }
699 break;
700 }
c627d613 701
dc5ddbcc
AT
702 file = &flist->files[i];
703 fname = file->name;
c627d613
AT
704
705 if (local_name)
706 fname = local_name;
707
708 if (dry_run) {
709 if (!am_server && verbose)
710 printf("%s\n",fname);
711 continue;
712 }
713
714 if (verbose > 2)
dc5ddbcc 715 fprintf(FERROR,"recv_files(%s)\n",fname);
c627d613
AT
716
717 /* open the file */
ac1eb754 718 fd1 = open(fname,O_RDONLY);
c627d613 719
ac1eb754 720 if (fd1 != -1 && fstat(fd1,&st) != 0) {
dc5ddbcc 721 fprintf(FERROR,"fstat %s : %s\n",fname,strerror(errno));
7b8356d0 722 receive_data(f_in,NULL,-1,NULL);
c627d613 723 close(fd1);
7b8356d0 724 continue;
c627d613
AT
725 }
726
ac1eb754 727 if (fd1 != -1 && !S_ISREG(st.st_mode)) {
7b8356d0
AT
728 fprintf(FERROR,"%s : not a regular file (recv_files)\n",fname);
729 receive_data(f_in,NULL,-1,NULL);
c627d613 730 close(fd1);
7b8356d0 731 continue;
c627d613
AT
732 }
733
ac1eb754 734 if (fd1 != -1 && st.st_size > 0) {
c627d613 735 buf = map_file(fd1,st.st_size);
9e31c482
AT
736 if (verbose > 2)
737 fprintf(FERROR,"recv mapped %s of size %d\n",fname,(int)st.st_size);
c627d613
AT
738 } else {
739 buf = NULL;
740 }
741
c627d613 742 /* open tmp file */
13a1f792
AT
743 if (strlen(fname) > (MAXPATHLEN-8)) {
744 fprintf(FERROR,"filename too long\n");
fee64929 745 close(fd1);
13a1f792
AT
746 continue;
747 }
950ab32d
AT
748 if (tmpdir) {
749 char *f;
750 f = strrchr(fname,'/');
751 if (f == NULL)
752 f = fname;
753 else
754 f++;
755 sprintf(fnametmp,"%s/%s.XXXXXX",tmpdir,f);
756 } else {
757 sprintf(fnametmp,"%s.XXXXXX",fname);
758 }
c627d613 759 if (NULL == mktemp(fnametmp)) {
dc5ddbcc 760 fprintf(FERROR,"mktemp %s failed\n",fnametmp);
7b8356d0
AT
761 receive_data(f_in,buf,-1,NULL);
762 if (buf) unmap_file(buf);
763 close(fd1);
764 continue;
c627d613 765 }
950ab32d 766 fd2 = open(fnametmp,O_WRONLY|O_CREAT|O_EXCL,file->mode);
fee64929 767 if (fd2 == -1 && relative_paths && errno == ENOENT &&
6574b4f7 768 create_directory_path(fnametmp) == 0) {
950ab32d 769 fd2 = open(fnametmp,O_WRONLY|O_CREAT|O_EXCL,file->mode);
6574b4f7 770 }
c627d613 771 if (fd2 == -1) {
dc5ddbcc 772 fprintf(FERROR,"open %s : %s\n",fnametmp,strerror(errno));
7b8356d0
AT
773 receive_data(f_in,buf,-1,NULL);
774 if (buf) unmap_file(buf);
775 close(fd1);
776 continue;
c627d613
AT
777 }
778
779 cleanup_fname = fnametmp;
780
781 if (!am_server && verbose)
782 printf("%s\n",fname);
783
784 /* recv file data */
9e31c482 785 recv_ok = receive_data(f_in,buf,fd2,fname);
c627d613 786
981791bd 787 if (fd1 != -1) {
1cdc8b50 788 if (buf) unmap_file(buf);
981791bd
AT
789 close(fd1);
790 }
c627d613
AT
791 close(fd2);
792
793 if (verbose > 2)
dc5ddbcc 794 fprintf(FERROR,"renaming %s to %s\n",fnametmp,fname);
c627d613
AT
795
796 if (make_backups) {
797 char fnamebak[MAXPATHLEN];
13a1f792
AT
798 if (strlen(fname) + strlen(backup_suffix) > (MAXPATHLEN-1)) {
799 fprintf(FERROR,"backup filename too long\n");
800 continue;
801 }
c627d613 802 sprintf(fnamebak,"%s%s",fname,backup_suffix);
ac1eb754 803 if (rename(fname,fnamebak) != 0 && errno != ENOENT) {
dc5ddbcc 804 fprintf(FERROR,"rename %s %s : %s\n",fname,fnamebak,strerror(errno));
7b8356d0 805 continue;
c627d613
AT
806 }
807 }
808
809 /* move tmp file over real file */
810 if (rename(fnametmp,fname) != 0) {
950ab32d
AT
811 if (errno == EXDEV) {
812 /* rename failed on cross-filesystem link.
813 Copy the file instead. */
13dc412d 814 if (copy_file(fnametmp,fname, file->mode)) {
950ab32d
AT
815 fprintf(FERROR,"copy %s -> %s : %s\n",
816 fnametmp,fname,strerror(errno));
13dc412d
AT
817 } else {
818 set_perms(fname,file,NULL,0);
819 }
950ab32d
AT
820 unlink(fnametmp);
821 } else {
822 fprintf(FERROR,"rename %s -> %s : %s\n",
823 fnametmp,fname,strerror(errno));
824 unlink(fnametmp);
825 }
13dc412d
AT
826 } else {
827 set_perms(fname,file,NULL,0);
c627d613
AT
828 }
829
830 cleanup_fname = NULL;
831
9e31c482
AT
832
833 if (!recv_ok) {
834 if (verbose > 1)
835 fprintf(FERROR,"redoing %s(%d)\n",fname,i);
52296954
AT
836 if (csum_length == SUM_LENGTH)
837 fprintf(FERROR,"ERROR: file corruption in %s\n",fname);
9e31c482
AT
838 write_int(f_gen,i);
839 }
c627d613
AT
840 }
841
7c596906
AT
842 if (preserve_hard_links)
843 do_hard_links(flist);
844
7b8356d0
AT
845 /* now we need to fix any directory permissions that were
846 modified during the transfer */
7c596906
AT
847 for (i = 0; i < flist->count; i++) {
848 struct file_struct *file = &flist->files[i];
849 if (!file->name || !S_ISDIR(file->mode)) continue;
850 recv_generator(file->name,flist,i,-1);
7b8356d0 851 }
7b8356d0 852
c627d613 853 if (verbose > 2)
dc5ddbcc 854 fprintf(FERROR,"recv_files finished\n");
c627d613
AT
855
856 return 0;
857}
858
859
860
861off_t send_files(struct file_list *flist,int f_out,int f_in)
862{
863 int fd;
864 struct sum_struct *s;
c6e7fcb4 865 struct map_struct *buf;
c627d613
AT
866 struct stat st;
867 char fname[MAXPATHLEN];
868 off_t total=0;
869 int i;
dc5ddbcc 870 struct file_struct *file;
c6e7fcb4 871 int phase = 0;
c627d613
AT
872
873 if (verbose > 2)
dc5ddbcc 874 fprintf(FERROR,"send_files starting\n");
c627d613 875
720b47f2
AT
876 setup_nonblocking(f_in,f_out);
877
c627d613
AT
878 while (1)
879 {
880 i = read_int(f_in);
c6e7fcb4
AT
881 if (i == -1) {
882 if (phase==0 && remote_version >= 13) {
883 phase++;
9e31c482 884 csum_length = SUM_LENGTH;
c6e7fcb4
AT
885 write_int(f_out,-1);
886 write_flush(f_out);
9e31c482
AT
887 if (verbose > 2)
888 fprintf(FERROR,"send_files phase=%d\n",phase);
c6e7fcb4
AT
889 continue;
890 }
891 break;
892 }
c627d613 893
dc5ddbcc
AT
894 file = &flist->files[i];
895
c627d613 896 fname[0] = 0;
dc5ddbcc 897 if (file->dir) {
13a1f792
AT
898 strncpy(fname,file->dir,MAXPATHLEN-1);
899 fname[MAXPATHLEN-1] = 0;
684b4e31
AT
900 if (strlen(fname) == MAXPATHLEN-1) {
901 fprintf(FERROR, "send_files failed on long-named directory %s\n",
902 fname);
903 return -1;
904 }
c627d613
AT
905 strcat(fname,"/");
906 }
13a1f792 907 strncat(fname,file->name,MAXPATHLEN-strlen(fname));
c627d613
AT
908
909 if (verbose > 2)
dc5ddbcc 910 fprintf(FERROR,"send_files(%d,%s)\n",i,fname);
c627d613
AT
911
912 if (dry_run) {
913 if (!am_server && verbose)
914 printf("%s\n",fname);
915 write_int(f_out,i);
916 continue;
917 }
918
919 s = receive_sums(f_in);
920 if (!s) {
dc5ddbcc 921 fprintf(FERROR,"receive_sums failed\n");
c627d613
AT
922 return -1;
923 }
924
925 fd = open(fname,O_RDONLY);
926 if (fd == -1) {
dc5ddbcc 927 fprintf(FERROR,"send_files failed to open %s: %s\n",
c627d613
AT
928 fname,strerror(errno));
929 continue;
930 }
931
932 /* map the local file */
933 if (fstat(fd,&st) != 0) {
dc5ddbcc 934 fprintf(FERROR,"fstat failed : %s\n",strerror(errno));
fee64929 935 close(fd);
c627d613
AT
936 return -1;
937 }
938
939 if (st.st_size > 0) {
940 buf = map_file(fd,st.st_size);
c627d613
AT
941 } else {
942 buf = NULL;
943 }
944
945 if (verbose > 2)
dc5ddbcc 946 fprintf(FERROR,"send_files mapped %s of size %d\n",
c627d613
AT
947 fname,(int)st.st_size);
948
949 write_int(f_out,i);
950
951 write_int(f_out,s->count);
952 write_int(f_out,s->n);
953 write_int(f_out,s->remainder);
954
955 if (verbose > 2)
dc5ddbcc 956 fprintf(FERROR,"calling match_sums %s\n",fname);
c627d613
AT
957
958 if (!am_server && verbose)
959 printf("%s\n",fname);
960
720b47f2 961 match_sums(f_out,s,buf,st.st_size);
c627d613
AT
962 write_flush(f_out);
963
1cdc8b50 964 if (buf) unmap_file(buf);
c627d613
AT
965 close(fd);
966
967 free_sums(s);
968
969 if (verbose > 2)
dc5ddbcc 970 fprintf(FERROR,"sender finished %s\n",fname);
c627d613
AT
971
972 total += st.st_size;
973 }
974
dc5ddbcc
AT
975 if (verbose > 2)
976 fprintf(FERROR,"send files finished\n");
977
c627d613
AT
978 match_report();
979
980 write_int(f_out,-1);
981 write_flush(f_out);
982
983 return total;
984}
985
986
987
c6e7fcb4 988void generate_files(int f,struct file_list *flist,char *local_name,int f_recv)
c627d613
AT
989{
990 int i;
9e31c482 991 int phase=0;
c627d613
AT
992
993 if (verbose > 2)
dc5ddbcc 994 fprintf(FERROR,"generator starting pid=%d count=%d\n",
c627d613
AT
995 (int)getpid(),flist->count);
996
997 for (i = 0; i < flist->count; i++) {
dc5ddbcc 998 struct file_struct *file = &flist->files[i];
7b8356d0 999 mode_t saved_mode = file->mode;
dc5ddbcc 1000 if (!file->name) continue;
7b8356d0
AT
1001
1002 /* we need to ensure that any directories we create have writeable
1003 permissions initially so that we can create the files within
1004 them. This is then fixed after the files are transferred */
1005 if (!am_root && S_ISDIR(file->mode)) {
1006 file->mode |= S_IWUSR; /* user write */
c627d613 1007 }
7b8356d0 1008
dc5ddbcc 1009 recv_generator(local_name?local_name:file->name,
c627d613 1010 flist,i,f);
7b8356d0
AT
1011
1012 file->mode = saved_mode;
c627d613 1013 }
c6e7fcb4 1014
9e31c482
AT
1015 phase++;
1016 csum_length = SUM_LENGTH;
1017 ignore_times=1;
1018
1019 if (verbose > 2)
1020 fprintf(FERROR,"generate_files phase=%d\n",phase);
1021
c627d613
AT
1022 write_int(f,-1);
1023 write_flush(f);
c6e7fcb4
AT
1024
1025 if (remote_version >= 13) {
7b8356d0
AT
1026 /* in newer versions of the protocol the files can cycle through
1027 the system more than once to catch initial checksum errors */
c6e7fcb4
AT
1028 for (i=read_int(f_recv); i != -1; i=read_int(f_recv)) {
1029 struct file_struct *file = &flist->files[i];
1030 recv_generator(local_name?local_name:file->name,
1031 flist,i,f);
1032 }
1033
9e31c482
AT
1034 phase++;
1035 if (verbose > 2)
1036 fprintf(FERROR,"generate_files phase=%d\n",phase);
1037
c6e7fcb4
AT
1038 write_int(f,-1);
1039 write_flush(f);
1040 }
1041
1042
c627d613 1043 if (verbose > 2)
dc5ddbcc 1044 fprintf(FERROR,"generator wrote %d\n",write_total());
c627d613 1045}
dc5ddbcc
AT
1046
1047