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