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 block_size;
34 extern int update_only;
35 extern int make_backups;
36 extern int preserve_links;
37 extern int preserve_hard_links;
38 extern int preserve_perms;
39 extern int preserve_devices;
40 extern int preserve_uid;
41 extern int preserve_gid;
42 extern int preserve_times;
44 extern int ignore_times;
46 extern int delete_mode;
47 extern int cvs_exclude;
53 static void free_sums(struct sum_struct *s)
55 if (s->sums) free(s->sums);
62 send a sums struct down a fd
64 static void send_sums(struct sum_struct *s,int f_out)
68 /* tell the other guy how many we are going to be doing and how many
69 bytes there are in the last chunk */
70 write_int(f_out,s?s->count:0);
71 write_int(f_out,s?s->n:block_size);
72 write_int(f_out,s?s->remainder:0);
74 for (i=0;i<s->count;i++) {
75 write_int(f_out,s->sums[i].sum1);
76 write_buf(f_out,s->sums[i].sum2,csum_length);
83 generate a stream of signatures/checksums that describe a buffer
85 generate approximately one checksum every n bytes
87 static struct sum_struct *generate_sums(struct map_struct *buf,off_t len,int n)
93 int remainder = (len%block_len);
96 count = (len+(block_len-1))/block_len;
98 s = (struct sum_struct *)malloc(sizeof(*s));
99 if (!s) out_of_memory("generate_sums");
102 s->remainder = remainder;
112 fprintf(FERROR,"count=%d rem=%d n=%d flength=%d\n",
113 s->count,s->remainder,s->n,(int)s->flength);
115 s->sums = (struct sum_buf *)malloc(sizeof(s->sums[0])*s->count);
116 if (!s->sums) out_of_memory("generate_sums");
118 for (i=0;i<count;i++) {
120 char *map = map_ptr(buf,offset,n1);
122 s->sums[i].sum1 = get_checksum1(map,n1);
123 get_checksum2(map,n1,s->sums[i].sum2);
125 s->sums[i].offset = offset;
130 fprintf(FERROR,"chunk[%d] offset=%d len=%d sum1=%08x\n",
131 i,(int)s->sums[i].offset,s->sums[i].len,s->sums[i].sum1);
142 receive the checksums for a buffer
144 static struct sum_struct *receive_sums(int f)
146 struct sum_struct *s;
151 s = (struct sum_struct *)malloc(sizeof(*s));
152 if (!s) out_of_memory("receive_sums");
154 s->count = read_int(f);
156 s->remainder = read_int(f);
160 fprintf(FERROR,"count=%d n=%d rem=%d\n",
161 s->count,s->n,s->remainder);
168 s->sums = (struct sum_buf *)malloc(sizeof(s->sums[0])*s->count);
169 if (!s->sums) out_of_memory("receive_sums");
171 for (i=0;i<s->count;i++) {
172 s->sums[i].sum1 = read_int(f);
173 read_buf(f,s->sums[i].sum2,csum_length);
175 s->sums[i].offset = offset;
178 if (i == s->count-1 && s->remainder != 0) {
179 s->sums[i].len = s->remainder;
181 s->sums[i].len = s->n;
183 offset += s->sums[i].len;
186 fprintf(FERROR,"chunk[%d] len=%d offset=%d sum1=%08x\n",
187 i,s->sums[i].len,(int)s->sums[i].offset,s->sums[i].sum1);
196 static int set_perms(char *fname,struct file_struct *file,struct stat *st,
202 if (dry_run) return 0;
205 if (lstat(fname,&st2) != 0) {
206 fprintf(FERROR,"stat %s : %s\n",fname,strerror(errno));
212 if (preserve_times && !S_ISLNK(st->st_mode) &&
213 st->st_mtime != file->modtime) {
215 if (set_modtime(fname,file->modtime) != 0) {
216 fprintf(FERROR,"failed to set times on %s : %s\n",
217 fname,strerror(errno));
223 if (preserve_perms && !S_ISLNK(st->st_mode) &&
224 st->st_mode != file->mode) {
226 if (chmod(fname,file->mode) != 0) {
227 fprintf(FERROR,"failed to set permissions on %s : %s\n",
228 fname,strerror(errno));
234 if ((am_root && preserve_uid && st->st_uid != file->uid) ||
235 (preserve_gid && st->st_gid != file->gid)) {
238 (am_root&&preserve_uid)?file->uid:-1,
239 preserve_gid?file->gid:-1) != 0) {
240 if (verbose>1 || preserve_uid)
241 fprintf(FERROR,"chown %s : %s\n",fname,strerror(errno));
246 if (verbose > 1 && report) {
248 fprintf(FINFO,"%s\n",fname);
250 fprintf(FINFO,"%s is uptodate\n",fname);
256 void recv_generator(char *fname,struct file_list *flist,int i,int f_out)
260 struct map_struct *buf;
261 struct sum_struct *s;
262 char sum[MD4_SUM_LENGTH];
264 struct file_struct *file = &flist->files[i];
267 fprintf(FERROR,"recv_generator(%s,%d)\n",fname,i);
269 statret = lstat(fname,&st);
271 if (S_ISDIR(file->mode)) {
273 if (statret == 0 && !S_ISDIR(st.st_mode)) {
274 if (unlink(fname) != 0) {
275 fprintf(FERROR,"unlink %s : %s\n",fname,strerror(errno));
280 if (statret != 0 && mkdir(fname,file->mode) != 0 && errno != EEXIST)
281 fprintf(FERROR,"mkdir %s : %s\n",fname,strerror(errno));
282 if (set_perms(fname,file,NULL,0) && verbose)
283 fprintf(FINFO,"%s/\n",fname);
288 if (preserve_links && S_ISLNK(file->mode)) {
289 char lnk[MAXPATHLEN];
292 l = readlink(fname,lnk,MAXPATHLEN-1);
295 if (strcmp(lnk,file->link) == 0) {
296 set_perms(fname,file,&st,1);
301 if (!dry_run) unlink(fname);
302 if (!dry_run && symlink(file->link,fname) != 0) {
303 fprintf(FERROR,"link %s -> %s : %s\n",
304 fname,file->link,strerror(errno));
306 set_perms(fname,file,NULL,0);
308 fprintf(FINFO,"%s -> %s\n",
316 if (am_root && preserve_devices && IS_DEVICE(file->mode)) {
318 st.st_mode != file->mode ||
319 st.st_rdev != file->rdev) {
320 if (!dry_run) unlink(fname);
322 fprintf(FERROR,"mknod(%s,0%o,0x%x)\n",
323 fname,(int)file->mode,(int)file->rdev);
325 mknod(fname,file->mode,file->rdev) != 0) {
326 fprintf(FERROR,"mknod %s : %s\n",fname,strerror(errno));
328 set_perms(fname,file,NULL,0);
330 fprintf(FINFO,"%s\n",fname);
333 set_perms(fname,file,&st,1);
339 if (preserve_hard_links && check_hard_link(file)) {
341 fprintf(FINFO,"%s is a hard link\n",file->name);
345 if (!S_ISREG(file->mode)) {
346 fprintf(FERROR,"skipping non-regular file %s\n",fname);
351 if (errno == ENOENT) {
353 if (!dry_run) send_sums(NULL,f_out);
356 fprintf(FERROR,"recv_generator failed to open %s\n",fname);
361 if (!S_ISREG(st.st_mode)) {
362 /* its not a regular file on the receiving end, but it is on the
363 sending end. If its a directory then skip it (too dangerous to
364 do a recursive deletion??) otherwise try to unlink it */
365 if (S_ISDIR(st.st_mode)) {
366 fprintf(FERROR,"ERROR: %s is a directory\n",fname);
369 if (unlink(fname) != 0) {
370 fprintf(FERROR,"%s : not a regular file (generator)\n",fname);
374 /* now pretend the file didn't exist */
376 if (!dry_run) send_sums(NULL,f_out);
380 if (update_only && st.st_mtime >= file->modtime) {
382 fprintf(FERROR,"%s is newer\n",fname);
386 if (always_checksum && S_ISREG(st.st_mode)) {
387 file_checksum(fname,sum,st.st_size);
390 if (st.st_size == file->length &&
391 ((!ignore_times && st.st_mtime == file->modtime) ||
392 (always_checksum && S_ISREG(st.st_mode) &&
393 memcmp(sum,file->sum,csum_length) == 0))) {
394 set_perms(fname,file,&st,1);
404 fd = open(fname,O_RDONLY);
407 fprintf(FERROR,"failed to open %s : %s\n",fname,strerror(errno));
411 if (st.st_size > 0) {
412 buf = map_file(fd,st.st_size);
418 fprintf(FERROR,"gen mapped %s of size %d\n",fname,(int)st.st_size);
420 s = generate_sums(buf,st.st_size,block_size);
423 fprintf(FERROR,"sending sums for %d\n",i);
430 if (buf) unmap_file(buf);
437 static int receive_data(int f_in,struct map_struct *buf,int fd,char *fname)
439 int i,n,remainder,len,count;
443 static char file_sum1[MD4_SUM_LENGTH];
444 static char file_sum2[MD4_SUM_LENGTH];
447 count = read_int(f_in);
449 remainder = read_int(f_in);
453 for (i=recv_token(f_in,&data); i != 0; i=recv_token(f_in,&data)) {
456 fprintf(FERROR,"data recv %d at %d\n",i,(int)offset);
460 if (fd != -1 && write_sparse(fd,data,i) != i) {
461 fprintf(FERROR,"write failed on %s : %s\n",fname,strerror(errno));
469 if (i == count-1 && remainder != 0)
473 fprintf(FERROR,"chunk[%d] of size %d at %d offset=%d\n",
474 i,len,(int)offset2,(int)offset);
476 map = map_ptr(buf,offset2,len);
481 if (fd != -1 && write_sparse(fd,map,len) != len) {
482 fprintf(FERROR,"write failed on %s : %s\n",fname,strerror(errno));
489 if (fd != -1 && offset > 0 && sparse_end(fd) != 0) {
490 fprintf(FERROR,"write failed on %s : %s\n",fname,strerror(errno));
496 if (remote_version >= 14) {
497 read_buf(f_in,file_sum2,MD4_SUM_LENGTH);
499 fprintf(FERROR,"got file_sum\n");
500 if (fd != -1 && memcmp(file_sum1,file_sum2,MD4_SUM_LENGTH) != 0)
507 static void delete_one(struct file_struct *f)
509 if (!S_ISDIR(f->mode)) {
510 if (!dry_run && unlink(f->name) != 0) {
511 fprintf(FERROR,"unlink %s : %s\n",f->name,strerror(errno));
512 } else if (verbose) {
513 fprintf(FERROR,"deleting %s\n",f->name);
516 if (!dry_run && rmdir(f->name) != 0) {
517 if (errno != ENOTEMPTY)
518 fprintf(FERROR,"rmdir %s : %s\n",f->name,strerror(errno));
519 } else if (verbose) {
520 fprintf(FERROR,"deleting directory %s\n",f->name);
526 static void delete_files(struct file_list *flist)
528 struct file_list *local_file_list;
535 if (!(local_file_list = send_file_list(-1,1,&dot)))
538 for (i=local_file_list->count;i>=0;i--) {
539 if (!local_file_list->files[i].name) continue;
540 if (-1 == flist_find(flist,&local_file_list->files[i])) {
541 delete_one(&local_file_list->files[i]);
546 static char *cleanup_fname = NULL;
548 void exit_cleanup(int code)
551 unlink(cleanup_fname);
561 int recv_files(int f_in,struct file_list *flist,char *local_name,int f_gen)
566 char fnametmp[MAXPATHLEN];
567 struct map_struct *buf;
569 struct file_struct *file;
574 fprintf(FERROR,"recv_files(%d) starting\n",flist->count);
577 if (recurse && delete_mode && !local_name && flist->count>0) {
585 if (phase==0 && remote_version >= 13) {
587 csum_length = SUM_LENGTH;
589 fprintf(FERROR,"recv_files phase=%d\n",phase);
597 file = &flist->files[i];
604 if (!am_server && verbose)
605 printf("%s\n",fname);
610 fprintf(FERROR,"recv_files(%s)\n",fname);
613 fd1 = open(fname,O_RDONLY);
615 if (fd1 != -1 && fstat(fd1,&st) != 0) {
616 fprintf(FERROR,"fstat %s : %s\n",fname,strerror(errno));
617 receive_data(f_in,NULL,-1,NULL);
622 if (fd1 != -1 && !S_ISREG(st.st_mode)) {
623 fprintf(FERROR,"%s : not a regular file (recv_files)\n",fname);
624 receive_data(f_in,NULL,-1,NULL);
629 if (fd1 != -1 && st.st_size > 0) {
630 buf = map_file(fd1,st.st_size);
632 fprintf(FERROR,"recv mapped %s of size %d\n",fname,(int)st.st_size);
638 sprintf(fnametmp,"%s.XXXXXX",fname);
639 if (NULL == mktemp(fnametmp)) {
640 fprintf(FERROR,"mktemp %s failed\n",fnametmp);
641 receive_data(f_in,buf,-1,NULL);
642 if (buf) unmap_file(buf);
646 fd2 = open(fnametmp,O_WRONLY|O_CREAT,file->mode);
648 fprintf(FERROR,"open %s : %s\n",fnametmp,strerror(errno));
649 receive_data(f_in,buf,-1,NULL);
650 if (buf) unmap_file(buf);
655 cleanup_fname = fnametmp;
657 if (!am_server && verbose)
658 printf("%s\n",fname);
661 recv_ok = receive_data(f_in,buf,fd2,fname);
664 if (buf) unmap_file(buf);
670 fprintf(FERROR,"renaming %s to %s\n",fnametmp,fname);
673 char fnamebak[MAXPATHLEN];
674 sprintf(fnamebak,"%s%s",fname,backup_suffix);
675 if (rename(fname,fnamebak) != 0 && errno != ENOENT) {
676 fprintf(FERROR,"rename %s %s : %s\n",fname,fnamebak,strerror(errno));
681 /* move tmp file over real file */
682 if (rename(fnametmp,fname) != 0) {
683 fprintf(FERROR,"rename %s -> %s : %s\n",
684 fnametmp,fname,strerror(errno));
688 cleanup_fname = NULL;
690 set_perms(fname,file,NULL,0);
694 fprintf(FERROR,"redoing %s(%d)\n",fname,i);
695 if (csum_length == SUM_LENGTH)
696 fprintf(FERROR,"ERROR: file corruption in %s\n",fname);
701 /* now we need to fix any directory permissions that were
702 modified during the transfer */
704 for (i = 0; i < flist->count; i++) {
705 struct file_struct *file = &flist->files[i];
706 if (!file->name || !S_ISDIR(file->mode)) continue;
707 recv_generator(file->name,flist,i,-1);
713 fprintf(FERROR,"recv_files finished\n");
720 off_t send_files(struct file_list *flist,int f_out,int f_in)
723 struct sum_struct *s;
724 struct map_struct *buf;
726 char fname[MAXPATHLEN];
729 struct file_struct *file;
733 fprintf(FERROR,"send_files starting\n");
735 setup_nonblocking(f_in,f_out);
741 if (phase==0 && remote_version >= 13) {
743 csum_length = SUM_LENGTH;
747 fprintf(FERROR,"send_files phase=%d\n",phase);
753 file = &flist->files[i];
757 strcpy(fname,file->dir);
760 strcat(fname,file->name);
763 fprintf(FERROR,"send_files(%d,%s)\n",i,fname);
766 if (!am_server && verbose)
767 printf("%s\n",fname);
772 s = receive_sums(f_in);
774 fprintf(FERROR,"receive_sums failed\n");
778 fd = open(fname,O_RDONLY);
780 fprintf(FERROR,"send_files failed to open %s: %s\n",
781 fname,strerror(errno));
785 /* map the local file */
786 if (fstat(fd,&st) != 0) {
787 fprintf(FERROR,"fstat failed : %s\n",strerror(errno));
791 if (st.st_size > 0) {
792 buf = map_file(fd,st.st_size);
798 fprintf(FERROR,"send_files mapped %s of size %d\n",
799 fname,(int)st.st_size);
803 write_int(f_out,s->count);
804 write_int(f_out,s->n);
805 write_int(f_out,s->remainder);
808 fprintf(FERROR,"calling match_sums %s\n",fname);
810 if (!am_server && verbose)
811 printf("%s\n",fname);
813 match_sums(f_out,s,buf,st.st_size);
816 if (buf) unmap_file(buf);
822 fprintf(FERROR,"sender finished %s\n",fname);
828 fprintf(FERROR,"send files finished\n");
840 void generate_files(int f,struct file_list *flist,char *local_name,int f_recv)
846 fprintf(FERROR,"generator starting pid=%d count=%d\n",
847 (int)getpid(),flist->count);
849 for (i = 0; i < flist->count; i++) {
850 struct file_struct *file = &flist->files[i];
851 mode_t saved_mode = file->mode;
852 if (!file->name) continue;
854 /* we need to ensure that any directories we create have writeable
855 permissions initially so that we can create the files within
856 them. This is then fixed after the files are transferred */
857 if (!am_root && S_ISDIR(file->mode)) {
858 file->mode |= S_IWUSR; /* user write */
861 recv_generator(local_name?local_name:file->name,
864 file->mode = saved_mode;
868 csum_length = SUM_LENGTH;
872 fprintf(FERROR,"generate_files phase=%d\n",phase);
877 if (remote_version >= 13) {
878 /* in newer versions of the protocol the files can cycle through
879 the system more than once to catch initial checksum errors */
880 for (i=read_int(f_recv); i != -1; i=read_int(f_recv)) {
881 struct file_struct *file = &flist->files[i];
882 recv_generator(local_name?local_name:file->name,
888 fprintf(FERROR,"generate_files phase=%d\n",phase);
896 fprintf(FERROR,"generator wrote %d\n",write_total());