2 Copyright (C) Andrew Tridgell 1996
3 Copyright (C) Paul Mackerras 1996
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.
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.
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.
22 extern int csum_length;
26 extern int always_checksum;
27 extern time_t starttime;
29 extern int remote_version;
31 extern char *backup_suffix;
33 extern int whole_file;
34 extern int block_size;
35 extern int update_only;
36 extern int make_backups;
37 extern int preserve_links;
38 extern int preserve_hard_links;
39 extern int preserve_perms;
40 extern int preserve_devices;
41 extern int preserve_uid;
42 extern int preserve_gid;
43 extern int preserve_times;
45 extern int ignore_times;
47 extern int delete_mode;
48 extern int cvs_exclude;
50 extern int relative_paths;
55 static void free_sums(struct sum_struct *s)
57 if (s->sums) free(s->sums);
64 send a sums struct down a fd
66 static void send_sums(struct sum_struct *s,int f_out)
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);
76 for (i=0;i<s->count;i++) {
77 write_int(f_out,s->sums[i].sum1);
78 write_buf(f_out,s->sums[i].sum2,csum_length);
85 generate a stream of signatures/checksums that describe a buffer
87 generate approximately one checksum every n bytes
89 static struct sum_struct *generate_sums(struct map_struct *buf,off_t len,int n)
95 int remainder = (len%block_len);
98 count = (len+(block_len-1))/block_len;
100 s = (struct sum_struct *)malloc(sizeof(*s));
101 if (!s) out_of_memory("generate_sums");
104 s->remainder = remainder;
114 fprintf(FERROR,"count=%d rem=%d n=%d flength=%d\n",
115 s->count,s->remainder,s->n,(int)s->flength);
117 s->sums = (struct sum_buf *)malloc(sizeof(s->sums[0])*s->count);
118 if (!s->sums) out_of_memory("generate_sums");
120 for (i=0;i<count;i++) {
122 char *map = map_ptr(buf,offset,n1);
124 s->sums[i].sum1 = get_checksum1(map,n1);
125 get_checksum2(map,n1,s->sums[i].sum2);
127 s->sums[i].offset = offset;
132 fprintf(FERROR,"chunk[%d] offset=%d len=%d sum1=%08x\n",
133 i,(int)s->sums[i].offset,s->sums[i].len,s->sums[i].sum1);
144 receive the checksums for a buffer
146 static struct sum_struct *receive_sums(int f)
148 struct sum_struct *s;
152 s = (struct sum_struct *)malloc(sizeof(*s));
153 if (!s) out_of_memory("receive_sums");
155 s->count = read_int(f);
157 s->remainder = read_int(f);
161 fprintf(FERROR,"count=%d n=%d rem=%d\n",
162 s->count,s->n,s->remainder);
167 s->sums = (struct sum_buf *)malloc(sizeof(s->sums[0])*s->count);
168 if (!s->sums) out_of_memory("receive_sums");
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);
174 s->sums[i].offset = offset;
177 if (i == s->count-1 && s->remainder != 0) {
178 s->sums[i].len = s->remainder;
180 s->sums[i].len = s->n;
182 offset += s->sums[i].len;
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);
195 static int set_perms(char *fname,struct file_struct *file,struct stat *st,
201 if (dry_run) return 0;
204 if (link_stat(fname,&st2) != 0) {
205 fprintf(FERROR,"stat %s : %s\n",fname,strerror(errno));
211 if (preserve_times && !S_ISLNK(st->st_mode) &&
212 st->st_mtime != file->modtime) {
214 if (set_modtime(fname,file->modtime) != 0) {
215 fprintf(FERROR,"failed to set times on %s : %s\n",
216 fname,strerror(errno));
222 if (preserve_perms && !S_ISLNK(st->st_mode) &&
223 st->st_mode != file->mode) {
225 if (chmod(fname,file->mode) != 0) {
226 fprintf(FERROR,"failed to set permissions on %s : %s\n",
227 fname,strerror(errno));
233 if ((am_root && preserve_uid && st->st_uid != file->uid) ||
234 (preserve_gid && st->st_gid != file->gid)) {
237 (am_root&&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));
245 if (verbose > 1 && report) {
247 fprintf(FINFO,"%s\n",fname);
249 fprintf(FINFO,"%s is uptodate\n",fname);
255 /* choose whether to skip a particular file */
256 static int skip_file(char *fname,
257 struct file_struct *file, struct stat *st)
259 if (st->st_size != file->length) {
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);
275 return (st->st_mtime == file->modtime);
279 void recv_generator(char *fname,struct file_list *flist,int i,int f_out)
283 struct map_struct *buf;
284 struct sum_struct *s;
286 struct file_struct *file = &flist->files[i];
289 fprintf(FERROR,"recv_generator(%s,%d)\n",fname,i);
291 statret = link_stat(fname,&st);
293 if (S_ISDIR(file->mode)) {
295 if (statret == 0 && !S_ISDIR(st.st_mode)) {
296 if (unlink(fname) != 0) {
297 fprintf(FERROR,"unlink %s : %s\n",fname,strerror(errno));
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));
310 if (set_perms(fname,file,NULL,0) && verbose)
311 fprintf(FINFO,"%s/\n",fname);
315 if (preserve_links && S_ISLNK(file->mode)) {
317 char lnk[MAXPATHLEN];
320 l = readlink(fname,lnk,MAXPATHLEN-1);
323 if (strcmp(lnk,file->link) == 0) {
324 set_perms(fname,file,&st,1);
329 if (!dry_run) unlink(fname);
330 if (!dry_run && symlink(file->link,fname) != 0) {
331 fprintf(FERROR,"link %s -> %s : %s\n",
332 fname,file->link,strerror(errno));
334 set_perms(fname,file,NULL,0);
336 fprintf(FINFO,"%s -> %s\n",
344 if (am_root && preserve_devices && IS_DEVICE(file->mode)) {
346 st.st_mode != file->mode ||
347 st.st_rdev != file->rdev) {
348 if (!dry_run) unlink(fname);
350 fprintf(FERROR,"mknod(%s,0%o,0x%x)\n",
351 fname,(int)file->mode,(int)file->rdev);
353 mknod(fname,file->mode,file->rdev) != 0) {
354 fprintf(FERROR,"mknod %s : %s\n",fname,strerror(errno));
356 set_perms(fname,file,NULL,0);
358 fprintf(FINFO,"%s\n",fname);
361 set_perms(fname,file,&st,1);
367 if (preserve_hard_links && check_hard_link(file)) {
369 fprintf(FINFO,"%s is a hard link\n",file->name);
373 if (!S_ISREG(file->mode)) {
374 fprintf(FERROR,"skipping non-regular file %s\n",fname);
379 if (errno == ENOENT) {
381 if (!dry_run) send_sums(NULL,f_out);
384 fprintf(FERROR,"recv_generator failed to open %s\n",fname);
389 if (!S_ISREG(st.st_mode)) {
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);
397 if (unlink(fname) != 0) {
398 fprintf(FERROR,"%s : not a regular file (generator)\n",fname);
402 /* now pretend the file didn't exist */
404 if (!dry_run) send_sums(NULL,f_out);
408 if (update_only && st.st_mtime >= file->modtime) {
410 fprintf(FERROR,"%s is newer\n",fname);
414 if (skip_file(fname, file, &st)) {
415 set_perms(fname,file,&st,1);
426 send_sums(NULL,f_out);
431 fd = open(fname,O_RDONLY);
434 fprintf(FERROR,"failed to open %s : %s\n",fname,strerror(errno));
435 fprintf(FERROR,"skipping %s\n",fname);
439 if (st.st_size > 0) {
440 buf = map_file(fd,st.st_size);
446 fprintf(FERROR,"gen mapped %s of size %d\n",fname,(int)st.st_size);
448 s = generate_sums(buf,st.st_size,block_size);
451 fprintf(FERROR,"sending sums for %d\n",i);
458 if (buf) unmap_file(buf);
465 static int receive_data(int f_in,struct map_struct *buf,int fd,char *fname)
467 int i,n,remainder,len,count;
471 static char file_sum1[MD4_SUM_LENGTH];
472 static char file_sum2[MD4_SUM_LENGTH];
475 count = read_int(f_in);
477 remainder = read_int(f_in);
481 for (i=recv_token(f_in,&data); i != 0; i=recv_token(f_in,&data)) {
484 fprintf(FERROR,"data recv %d at %d\n",i,(int)offset);
488 if (fd != -1 && write_sparse(fd,data,i) != i) {
489 fprintf(FERROR,"write failed on %s : %s\n",fname,strerror(errno));
497 if (i == count-1 && remainder != 0)
501 fprintf(FERROR,"chunk[%d] of size %d at %d offset=%d\n",
502 i,len,(int)offset2,(int)offset);
504 map = map_ptr(buf,offset2,len);
509 if (fd != -1 && write_sparse(fd,map,len) != len) {
510 fprintf(FERROR,"write failed on %s : %s\n",fname,strerror(errno));
517 if (fd != -1 && offset > 0 && sparse_end(fd) != 0) {
518 fprintf(FERROR,"write failed on %s : %s\n",fname,strerror(errno));
524 if (remote_version >= 14) {
525 read_buf(f_in,file_sum2,MD4_SUM_LENGTH);
527 fprintf(FERROR,"got file_sum\n");
528 if (fd != -1 && memcmp(file_sum1,file_sum2,MD4_SUM_LENGTH) != 0)
535 static void delete_one(struct file_struct *f)
537 if (!S_ISDIR(f->mode)) {
538 if (!dry_run && unlink(f->name) != 0) {
539 fprintf(FERROR,"unlink %s : %s\n",f->name,strerror(errno));
540 } else if (verbose) {
541 fprintf(FERROR,"deleting %s\n",f->name);
544 if (!dry_run && rmdir(f->name) != 0) {
545 if (errno != ENOTEMPTY)
546 fprintf(FERROR,"rmdir %s : %s\n",f->name,strerror(errno));
547 } else if (verbose) {
548 fprintf(FERROR,"deleting directory %s\n",f->name);
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.
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 */
561 static int delete_already_done(struct file_list *flist,int j)
567 if (j == 0) return 0;
569 name = strdup(flist->files[j].name);
572 fprintf(FERROR,"out of memory in delete_already_done");
576 p = strrchr(name,'/');
583 while (low != high) {
584 int mid = (low+high)/2;
585 int ret = strcmp(flist->files[flist_up(flist, mid)].name,name);
597 low = flist_up(flist, low);
599 if (strcmp(flist->files[low].name,name) == 0) {
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 */
612 static void delete_files(struct file_list *flist)
614 struct file_list *local_file_list;
620 for (j=0;j<flist->count;j++) {
621 char *name = flist->files[j].name;
623 if (!S_ISDIR(flist->files[j].mode)) continue;
625 if (delete_already_done(flist, j)) continue;
627 if (!(local_file_list = send_file_list(-1,1,&name)))
631 fprintf(FINFO,"deleting in %s\n", name);
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]);
642 static char *cleanup_fname = NULL;
644 void exit_cleanup(int code)
647 unlink(cleanup_fname);
648 signal(SIGUSR1, SIG_IGN);
651 kill(-getpgrp(), SIGUSR1);
653 kill(-getpgrp(getpid()), SIGUSR1);
665 int recv_files(int f_in,struct file_list *flist,char *local_name,int f_gen)
670 char fnametmp[MAXPATHLEN];
671 struct map_struct *buf;
673 struct file_struct *file;
678 fprintf(FERROR,"recv_files(%d) starting\n",flist->count);
681 if (recurse && delete_mode && !local_name && flist->count>0) {
689 if (phase==0 && remote_version >= 13) {
691 csum_length = SUM_LENGTH;
693 fprintf(FERROR,"recv_files phase=%d\n",phase);
701 file = &flist->files[i];
708 if (!am_server && verbose)
709 printf("%s\n",fname);
714 fprintf(FERROR,"recv_files(%s)\n",fname);
717 fd1 = open(fname,O_RDONLY);
719 if (fd1 != -1 && fstat(fd1,&st) != 0) {
720 fprintf(FERROR,"fstat %s : %s\n",fname,strerror(errno));
721 receive_data(f_in,NULL,-1,NULL);
726 if (fd1 != -1 && !S_ISREG(st.st_mode)) {
727 fprintf(FERROR,"%s : not a regular file (recv_files)\n",fname);
728 receive_data(f_in,NULL,-1,NULL);
733 if (fd1 != -1 && st.st_size > 0) {
734 buf = map_file(fd1,st.st_size);
736 fprintf(FERROR,"recv mapped %s of size %d\n",fname,(int)st.st_size);
742 if (strlen(fname) > (MAXPATHLEN-8)) {
743 fprintf(FERROR,"filename too long\n");
747 sprintf(fnametmp,"%s.XXXXXX",fname);
748 if (NULL == mktemp(fnametmp)) {
749 fprintf(FERROR,"mktemp %s failed\n",fnametmp);
750 receive_data(f_in,buf,-1,NULL);
751 if (buf) unmap_file(buf);
755 fd2 = open(fnametmp,O_WRONLY|O_CREAT,file->mode);
756 if (fd2 == -1 && relative_paths && errno == ENOENT &&
757 create_directory_path(fnametmp) == 0) {
758 fd2 = open(fnametmp,O_WRONLY|O_CREAT,file->mode);
761 fprintf(FERROR,"open %s : %s\n",fnametmp,strerror(errno));
762 receive_data(f_in,buf,-1,NULL);
763 if (buf) unmap_file(buf);
768 cleanup_fname = fnametmp;
770 if (!am_server && verbose)
771 printf("%s\n",fname);
774 recv_ok = receive_data(f_in,buf,fd2,fname);
777 if (buf) unmap_file(buf);
783 fprintf(FERROR,"renaming %s to %s\n",fnametmp,fname);
786 char fnamebak[MAXPATHLEN];
787 if (strlen(fname) + strlen(backup_suffix) > (MAXPATHLEN-1)) {
788 fprintf(FERROR,"backup filename too long\n");
791 sprintf(fnamebak,"%s%s",fname,backup_suffix);
792 if (rename(fname,fnamebak) != 0 && errno != ENOENT) {
793 fprintf(FERROR,"rename %s %s : %s\n",fname,fnamebak,strerror(errno));
798 /* move tmp file over real file */
799 if (rename(fnametmp,fname) != 0) {
800 fprintf(FERROR,"rename %s -> %s : %s\n",
801 fnametmp,fname,strerror(errno));
805 cleanup_fname = NULL;
807 set_perms(fname,file,NULL,0);
811 fprintf(FERROR,"redoing %s(%d)\n",fname,i);
812 if (csum_length == SUM_LENGTH)
813 fprintf(FERROR,"ERROR: file corruption in %s\n",fname);
818 if (preserve_hard_links)
819 do_hard_links(flist);
821 /* now we need to fix any directory permissions that were
822 modified during the transfer */
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);
830 fprintf(FERROR,"recv_files finished\n");
837 off_t send_files(struct file_list *flist,int f_out,int f_in)
840 struct sum_struct *s;
841 struct map_struct *buf;
843 char fname[MAXPATHLEN];
846 struct file_struct *file;
850 fprintf(FERROR,"send_files starting\n");
852 setup_nonblocking(f_in,f_out);
858 if (phase==0 && remote_version >= 13) {
860 csum_length = SUM_LENGTH;
864 fprintf(FERROR,"send_files phase=%d\n",phase);
870 file = &flist->files[i];
874 strncpy(fname,file->dir,MAXPATHLEN-1);
875 fname[MAXPATHLEN-1] = 0;
876 if (strlen(fname) == MAXPATHLEN-1) {
877 fprintf(FERROR, "send_files failed on long-named directory %s\n",
883 strncat(fname,file->name,MAXPATHLEN-strlen(fname));
886 fprintf(FERROR,"send_files(%d,%s)\n",i,fname);
889 if (!am_server && verbose)
890 printf("%s\n",fname);
895 s = receive_sums(f_in);
897 fprintf(FERROR,"receive_sums failed\n");
901 fd = open(fname,O_RDONLY);
903 fprintf(FERROR,"send_files failed to open %s: %s\n",
904 fname,strerror(errno));
908 /* map the local file */
909 if (fstat(fd,&st) != 0) {
910 fprintf(FERROR,"fstat failed : %s\n",strerror(errno));
915 if (st.st_size > 0) {
916 buf = map_file(fd,st.st_size);
922 fprintf(FERROR,"send_files mapped %s of size %d\n",
923 fname,(int)st.st_size);
927 write_int(f_out,s->count);
928 write_int(f_out,s->n);
929 write_int(f_out,s->remainder);
932 fprintf(FERROR,"calling match_sums %s\n",fname);
934 if (!am_server && verbose)
935 printf("%s\n",fname);
937 match_sums(f_out,s,buf,st.st_size);
940 if (buf) unmap_file(buf);
946 fprintf(FERROR,"sender finished %s\n",fname);
952 fprintf(FERROR,"send files finished\n");
964 void generate_files(int f,struct file_list *flist,char *local_name,int f_recv)
970 fprintf(FERROR,"generator starting pid=%d count=%d\n",
971 (int)getpid(),flist->count);
973 for (i = 0; i < flist->count; i++) {
974 struct file_struct *file = &flist->files[i];
975 mode_t saved_mode = file->mode;
976 if (!file->name) continue;
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 */
985 recv_generator(local_name?local_name:file->name,
988 file->mode = saved_mode;
992 csum_length = SUM_LENGTH;
996 fprintf(FERROR,"generate_files phase=%d\n",phase);
1001 if (remote_version >= 13) {
1002 /* in newer versions of the protocol the files can cycle through
1003 the system more than once to catch initial checksum errors */
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,
1012 fprintf(FERROR,"generate_files phase=%d\n",phase);
1020 fprintf(FERROR,"generator wrote %d\n",write_total());