*** empty log message ***
[rsync/rsync.git] / rsync.c
... / ...
CommitLineData
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
22extern int csum_length;
23
24extern int verbose;
25extern int am_server;
26extern int always_checksum;
27extern time_t starttime;
28
29extern int remote_version;
30
31extern char *backup_suffix;
32
33extern int block_size;
34extern int update_only;
35extern int make_backups;
36extern int preserve_links;
37extern int preserve_hard_links;
38extern int preserve_perms;
39extern int preserve_devices;
40extern int preserve_uid;
41extern int preserve_gid;
42extern int preserve_times;
43extern int dry_run;
44extern int ignore_times;
45extern int recurse;
46extern int delete_mode;
47extern int cvs_exclude;
48
49/*
50 free a sums struct
51 */
52static 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 */
63static 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);
75 write_buf(f_out,s->sums[i].sum2,csum_length);
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 */
86static struct sum_struct *generate_sums(struct map_struct *buf,off_t len,int n)
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)
111 fprintf(FERROR,"count=%d rem=%d n=%d flength=%d\n",
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);
119 char *map = map_ptr(buf,offset,n1);
120
121 s->sums[i].sum1 = get_checksum1(map,n1);
122 get_checksum2(map,n1,s->sums[i].sum2);
123
124 s->sums[i].offset = offset;
125 s->sums[i].len = n1;
126 s->sums[i].i = i;
127
128 if (verbose > 3)
129 fprintf(FERROR,"chunk[%d] offset=%d len=%d sum1=%08x\n",
130 i,(int)s->sums[i].offset,s->sums[i].len,s->sums[i].sum1);
131
132 len -= n1;
133 offset += n1;
134 }
135
136 return s;
137}
138
139
140/*
141 receive the checksums for a buffer
142 */
143static 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)
159 fprintf(FERROR,"count=%d n=%d rem=%d\n",
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);
172 read_buf(f,s->sums[i].sum2,csum_length);
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)
185 fprintf(FERROR,"chunk[%d] len=%d offset=%d sum1=%08x\n",
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
195static 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) {
205 fprintf(FERROR,"stat %s : %s\n",fname,strerror(errno));
206 return;
207 }
208 st = &st2;
209 }
210
211 if (preserve_times && !S_ISLNK(st->st_mode) &&
212 st->st_mtime != file->modtime) {
213 updated = 1;
214 if (set_modtime(fname,file->modtime) != 0) {
215 fprintf(FERROR,"failed to set times on %s : %s\n",
216 fname,strerror(errno));
217 return;
218 }
219 }
220
221#ifdef HAVE_CHMOD
222 if (preserve_perms && !S_ISLNK(st->st_mode) &&
223 st->st_mode != file->mode) {
224 updated = 1;
225 if (chmod(fname,file->mode) != 0) {
226 fprintf(FERROR,"failed to set permissions on %s : %s\n",
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)
240 fprintf(FERROR,"chown %s : %s\n",fname,strerror(errno));
241 return;
242 }
243 }
244
245 if (verbose > 1 && report) {
246 if (updated)
247 fprintf(FINFO,"%s\n",fname);
248 else
249 fprintf(FINFO,"%s is uptodate\n",fname);
250 }
251}
252
253
254void recv_generator(char *fname,struct file_list *flist,int i,int f_out)
255{
256 int fd;
257 struct stat st;
258 struct map_struct *buf;
259 struct sum_struct *s;
260 char sum[SUM_LENGTH];
261 int statret;
262 struct file_struct *file = &flist->files[i];
263
264 if (verbose > 2)
265 fprintf(FERROR,"recv_generator(%s)\n",fname);
266
267 statret = lstat(fname,&st);
268
269#if SUPPORT_LINKS
270 if (preserve_links && S_ISLNK(file->mode)) {
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;
277 if (strcmp(lnk,file->link) == 0) {
278 set_perms(fname,file,&st,1);
279 return;
280 }
281 }
282 }
283 if (!dry_run) unlink(fname);
284 if (!dry_run && symlink(file->link,fname) != 0) {
285 fprintf(FERROR,"link %s -> %s : %s\n",
286 fname,file->link,strerror(errno));
287 } else {
288 set_perms(fname,file,NULL,0);
289 if (verbose)
290 fprintf(FINFO,"%s -> %s\n",
291 fname,file->link);
292 }
293 return;
294 }
295#endif
296
297#ifdef HAVE_MKNOD
298 if (preserve_devices && IS_DEVICE(file->mode)) {
299 if (statret != 0 ||
300 st.st_mode != file->mode ||
301 st.st_rdev != file->rdev) {
302 if (!dry_run) unlink(fname);
303 if (verbose > 2)
304 fprintf(FERROR,"mknod(%s,0%o,0x%x)\n",
305 fname,(int)file->mode,(int)file->rdev);
306 if (!dry_run &&
307 mknod(fname,file->mode,file->rdev) != 0) {
308 fprintf(FERROR,"mknod %s : %s\n",fname,strerror(errno));
309 } else {
310 set_perms(fname,file,NULL,0);
311 if (verbose)
312 fprintf(FINFO,"%s\n",fname);
313 }
314 } else {
315 set_perms(fname,file,&st,1);
316 }
317 return;
318 }
319#endif
320
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);
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)
338 fprintf(FERROR,"recv_generator failed to open %s\n",fname);
339 }
340 return;
341 }
342
343 if (!S_ISREG(st.st_mode)) {
344 fprintf(FERROR,"%s : not a regular file\n",fname);
345 return;
346 }
347
348 if (update_only && st.st_mtime >= file->modtime) {
349 if (verbose > 1)
350 fprintf(FERROR,"%s is newer\n",fname);
351 return;
352 }
353
354 if (always_checksum && S_ISREG(st.st_mode)) {
355 file_checksum(fname,sum,st.st_size);
356 }
357
358 if (st.st_size == file->length &&
359 ((!ignore_times && st.st_mtime == file->modtime) ||
360 (always_checksum && S_ISREG(st.st_mode) &&
361 memcmp(sum,file->sum,csum_length) == 0))) {
362 set_perms(fname,file,&st,1);
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) {
375 fprintf(FERROR,"failed to open %s : %s\n",fname,strerror(errno));
376 return;
377 }
378
379 if (st.st_size > 0) {
380 buf = map_file(fd,st.st_size);
381 } else {
382 buf = NULL;
383 }
384
385 if (verbose > 3)
386 fprintf(FERROR,"mapped %s of size %d\n",fname,(int)st.st_size);
387
388 s = generate_sums(buf,st.st_size,block_size);
389
390 write_int(f_out,i);
391 send_sums(s,f_out);
392 write_flush(f_out);
393
394 close(fd);
395 unmap_file(buf);
396
397 free_sums(s);
398}
399
400
401
402static void receive_data(int f_in,struct map_struct *buf,int fd,char *fname)
403{
404 int i,n,remainder,len,count;
405 off_t offset = 0;
406 off_t offset2;
407 char *data;
408
409 count = read_int(f_in);
410 n = read_int(f_in);
411 remainder = read_int(f_in);
412
413 for (i=recv_token(f_in,&data); i != 0; i=recv_token(f_in,&data)) {
414 if (i > 0) {
415 if (verbose > 3)
416 fprintf(FERROR,"data recv %d at %d\n",i,(int)offset);
417
418 if (write_sparse(fd,data,i) != i) {
419 fprintf(FERROR,"write failed on %s : %s\n",fname,strerror(errno));
420 exit_cleanup(1);
421 }
422 offset += i;
423 } else {
424 i = -(i+1);
425 offset2 = i*n;
426 len = n;
427 if (i == count-1 && remainder != 0)
428 len = remainder;
429
430 if (verbose > 3)
431 fprintf(FERROR,"chunk[%d] of size %d at %d offset=%d\n",
432 i,len,(int)offset2,(int)offset);
433
434 if (write_sparse(fd,map_ptr(buf,offset2,len),len) != len) {
435 fprintf(FERROR,"write failed on %s : %s\n",fname,strerror(errno));
436 exit_cleanup(1);
437 }
438 offset += len;
439 }
440 }
441
442 if (offset > 0 && sparse_end(fd) != 0) {
443 fprintf(FERROR,"write failed on %s : %s\n",fname,strerror(errno));
444 exit_cleanup(1);
445 }
446}
447
448
449static void delete_one(struct file_struct *f)
450{
451 if (!S_ISDIR(f->mode)) {
452 if (!dry_run && unlink(f->name) != 0) {
453 fprintf(FERROR,"unlink %s : %s\n",f->name,strerror(errno));
454 } else if (verbose) {
455 fprintf(FERROR,"deleting %s\n",f->name);
456 }
457 } else {
458 if (!dry_run && rmdir(f->name) != 0) {
459 if (errno != ENOTEMPTY)
460 fprintf(FERROR,"rmdir %s : %s\n",f->name,strerror(errno));
461 } else if (verbose) {
462 fprintf(FERROR,"deleting directory %s\n",f->name);
463 }
464 }
465}
466
467
468static void delete_files(struct file_list *flist)
469{
470 struct file_list *local_file_list;
471 char *dot=".";
472 int i;
473
474 if (cvs_exclude)
475 add_cvs_excludes();
476
477 if (!(local_file_list = send_file_list(-1,recurse,1,&dot)))
478 return;
479
480 for (i=local_file_list->count;i>=0;i--) {
481 if (!local_file_list->files[i].name) continue;
482 if (-1 == flist_find(flist,&local_file_list->files[i])) {
483 delete_one(&local_file_list->files[i]);
484 }
485 }
486}
487
488static char *cleanup_fname = NULL;
489
490void exit_cleanup(int code)
491{
492 if (cleanup_fname)
493 unlink(cleanup_fname);
494 exit(code);
495}
496
497void sig_int(void)
498{
499 exit_cleanup(1);
500}
501
502
503int recv_files(int f_in,struct file_list *flist,char *local_name,int f_gen)
504{
505 int fd1,fd2;
506 struct stat st;
507 char *fname;
508 char fnametmp[MAXPATHLEN];
509 struct map_struct *buf;
510 int i;
511 struct file_struct *file;
512 int phase=0;
513
514 if (verbose > 2) {
515 fprintf(FERROR,"recv_files(%d) starting\n",flist->count);
516 }
517
518 if (recurse && delete_mode && !local_name && flist->count>0) {
519 delete_files(flist);
520 }
521
522 while (1)
523 {
524 i = read_int(f_in);
525 if (i == -1) {
526 if (phase==0 && remote_version >= 13) {
527 phase++;
528 write_int(f_gen,-1);
529 write_flush(f_gen);
530 continue;
531 }
532 break;
533 }
534
535 file = &flist->files[i];
536 fname = file->name;
537
538 if (local_name)
539 fname = local_name;
540
541 if (dry_run) {
542 if (!am_server && verbose)
543 printf("%s\n",fname);
544 continue;
545 }
546
547 if (verbose > 2)
548 fprintf(FERROR,"recv_files(%s)\n",fname);
549
550 /* open the file */
551 fd1 = open(fname,O_RDONLY);
552
553 if (fd1 != -1 && fstat(fd1,&st) != 0) {
554 fprintf(FERROR,"fstat %s : %s\n",fname,strerror(errno));
555 close(fd1);
556 return -1;
557 }
558
559 if (fd1 != -1 && !S_ISREG(st.st_mode)) {
560 fprintf(FERROR,"%s : not a regular file\n",fname);
561 close(fd1);
562 return -1;
563 }
564
565 if (fd1 != -1 && st.st_size > 0) {
566 buf = map_file(fd1,st.st_size);
567 } else {
568 buf = NULL;
569 }
570
571 if (verbose > 2)
572 fprintf(FERROR,"mapped %s of size %d\n",fname,(int)st.st_size);
573
574 /* open tmp file */
575 sprintf(fnametmp,"%s.XXXXXX",fname);
576 if (NULL == mktemp(fnametmp)) {
577 fprintf(FERROR,"mktemp %s failed\n",fnametmp);
578 return -1;
579 }
580 fd2 = open(fnametmp,O_WRONLY|O_CREAT,file->mode);
581 if (fd2 == -1) {
582 fprintf(FERROR,"open %s : %s\n",fnametmp,strerror(errno));
583 return -1;
584 }
585
586 cleanup_fname = fnametmp;
587
588 if (!am_server && verbose)
589 printf("%s\n",fname);
590
591 /* recv file data */
592 receive_data(f_in,buf,fd2,fname);
593
594 if (fd1 != -1) {
595 unmap_file(buf);
596 close(fd1);
597 }
598 close(fd2);
599
600 if (verbose > 2)
601 fprintf(FERROR,"renaming %s to %s\n",fnametmp,fname);
602
603 if (make_backups) {
604 char fnamebak[MAXPATHLEN];
605 sprintf(fnamebak,"%s%s",fname,backup_suffix);
606 if (rename(fname,fnamebak) != 0 && errno != ENOENT) {
607 fprintf(FERROR,"rename %s %s : %s\n",fname,fnamebak,strerror(errno));
608 exit_cleanup(1);
609 }
610 }
611
612 /* move tmp file over real file */
613 if (rename(fnametmp,fname) != 0) {
614 fprintf(FERROR,"rename %s -> %s : %s\n",
615 fnametmp,fname,strerror(errno));
616 }
617
618 cleanup_fname = NULL;
619
620 set_perms(fname,file,NULL,0);
621 }
622
623 if (verbose > 2)
624 fprintf(FERROR,"recv_files finished\n");
625
626 return 0;
627}
628
629
630
631off_t send_files(struct file_list *flist,int f_out,int f_in)
632{
633 int fd;
634 struct sum_struct *s;
635 struct map_struct *buf;
636 struct stat st;
637 char fname[MAXPATHLEN];
638 off_t total=0;
639 int i;
640 struct file_struct *file;
641 int phase = 0;
642
643 if (verbose > 2)
644 fprintf(FERROR,"send_files starting\n");
645
646 setup_nonblocking(f_in,f_out);
647
648 while (1)
649 {
650 i = read_int(f_in);
651 if (i == -1) {
652 if (phase==0 && remote_version >= 13) {
653 phase++;
654 write_int(f_out,-1);
655 write_flush(f_out);
656 continue;
657 }
658 break;
659 }
660
661 file = &flist->files[i];
662
663 fname[0] = 0;
664 if (file->dir) {
665 strcpy(fname,file->dir);
666 strcat(fname,"/");
667 }
668 strcat(fname,file->name);
669
670 if (verbose > 2)
671 fprintf(FERROR,"send_files(%d,%s)\n",i,fname);
672
673 if (dry_run) {
674 if (!am_server && verbose)
675 printf("%s\n",fname);
676 write_int(f_out,i);
677 continue;
678 }
679
680 s = receive_sums(f_in);
681 if (!s) {
682 fprintf(FERROR,"receive_sums failed\n");
683 return -1;
684 }
685
686 fd = open(fname,O_RDONLY);
687 if (fd == -1) {
688 fprintf(FERROR,"send_files failed to open %s: %s\n",
689 fname,strerror(errno));
690 continue;
691 }
692
693 /* map the local file */
694 if (fstat(fd,&st) != 0) {
695 fprintf(FERROR,"fstat failed : %s\n",strerror(errno));
696 return -1;
697 }
698
699 if (st.st_size > 0) {
700 buf = map_file(fd,st.st_size);
701 } else {
702 buf = NULL;
703 }
704
705 if (verbose > 2)
706 fprintf(FERROR,"send_files mapped %s of size %d\n",
707 fname,(int)st.st_size);
708
709 write_int(f_out,i);
710
711 write_int(f_out,s->count);
712 write_int(f_out,s->n);
713 write_int(f_out,s->remainder);
714
715 if (verbose > 2)
716 fprintf(FERROR,"calling match_sums %s\n",fname);
717
718 if (!am_server && verbose)
719 printf("%s\n",fname);
720
721 match_sums(f_out,s,buf,st.st_size);
722 write_flush(f_out);
723
724 unmap_file(buf);
725 close(fd);
726
727 free_sums(s);
728
729 if (verbose > 2)
730 fprintf(FERROR,"sender finished %s\n",fname);
731
732 total += st.st_size;
733 }
734
735 if (verbose > 2)
736 fprintf(FERROR,"send files finished\n");
737
738 match_report();
739
740 write_int(f_out,-1);
741 write_flush(f_out);
742
743 return total;
744}
745
746
747
748void generate_files(int f,struct file_list *flist,char *local_name,int f_recv)
749{
750 int i;
751
752 if (verbose > 2)
753 fprintf(FERROR,"generator starting pid=%d count=%d\n",
754 (int)getpid(),flist->count);
755
756 for (i = 0; i < flist->count; i++) {
757 struct file_struct *file = &flist->files[i];
758 if (!file->name) continue;
759 if (S_ISDIR(file->mode)) {
760 if (dry_run) continue;
761 if (mkdir(file->name,file->mode) != 0 &&
762 errno != EEXIST) {
763 fprintf(FERROR,"mkdir %s : %s\n",
764 file->name,strerror(errno));
765 }
766 continue;
767 }
768 recv_generator(local_name?local_name:file->name,
769 flist,i,f);
770 }
771
772 write_int(f,-1);
773 write_flush(f);
774
775 if (remote_version >= 13) {
776 /* go to the full checksum if anything has failed so far */
777 csum_length = SUM_LENGTH;
778
779 for (i=read_int(f_recv); i != -1; i=read_int(f_recv)) {
780 struct file_struct *file = &flist->files[i];
781 recv_generator(local_name?local_name:file->name,
782 flist,i,f);
783 }
784
785 write_int(f,-1);
786 write_flush(f);
787 }
788
789
790 if (verbose > 2)
791 fprintf(FERROR,"generator wrote %d\n",write_total());
792}
793
794