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;
49 extern int relative_paths;
54 static void free_sums(struct sum_struct *s)
56 if (s->sums) free(s->sums);
63 send a sums struct down a fd
65 static void send_sums(struct sum_struct *s,int f_out)
69 /* tell the other guy how many we are going to be doing and how many
70 bytes there are in the last chunk */
71 write_int(f_out,s?s->count:0);
72 write_int(f_out,s?s->n:block_size);
73 write_int(f_out,s?s->remainder:0);
75 for (i=0;i<s->count;i++) {
76 write_int(f_out,s->sums[i].sum1);
77 write_buf(f_out,s->sums[i].sum2,csum_length);
84 generate a stream of signatures/checksums that describe a buffer
86 generate approximately one checksum every n bytes
88 static struct sum_struct *generate_sums(struct map_struct *buf,off_t len,int n)
94 int remainder = (len%block_len);
97 count = (len+(block_len-1))/block_len;
99 s = (struct sum_struct *)malloc(sizeof(*s));
100 if (!s) out_of_memory("generate_sums");
103 s->remainder = remainder;
113 fprintf(FERROR,"count=%d rem=%d n=%d flength=%d\n",
114 s->count,s->remainder,s->n,(int)s->flength);
116 s->sums = (struct sum_buf *)malloc(sizeof(s->sums[0])*s->count);
117 if (!s->sums) out_of_memory("generate_sums");
119 for (i=0;i<count;i++) {
121 char *map = map_ptr(buf,offset,n1);
123 s->sums[i].sum1 = get_checksum1(map,n1);
124 get_checksum2(map,n1,s->sums[i].sum2);
126 s->sums[i].offset = offset;
131 fprintf(FERROR,"chunk[%d] offset=%d len=%d sum1=%08x\n",
132 i,(int)s->sums[i].offset,s->sums[i].len,s->sums[i].sum1);
143 receive the checksums for a buffer
145 static struct sum_struct *receive_sums(int f)
147 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);
169 s->sums = (struct sum_buf *)malloc(sizeof(s->sums[0])*s->count);
170 if (!s->sums) out_of_memory("receive_sums");
172 for (i=0;i<s->count;i++) {
173 s->sums[i].sum1 = read_int(f);
174 read_buf(f,s->sums[i].sum2,csum_length);
176 s->sums[i].offset = offset;
179 if (i == s->count-1 && s->remainder != 0) {
180 s->sums[i].len = s->remainder;
182 s->sums[i].len = s->n;
184 offset += s->sums[i].len;
187 fprintf(FERROR,"chunk[%d] len=%d offset=%d sum1=%08x\n",
188 i,s->sums[i].len,(int)s->sums[i].offset,s->sums[i].sum1);
197 static int set_perms(char *fname,struct file_struct *file,struct stat *st,
203 if (dry_run) return 0;
206 if (lstat(fname,&st2) != 0) {
207 fprintf(FERROR,"stat %s : %s\n",fname,strerror(errno));
213 if (preserve_times && !S_ISLNK(st->st_mode) &&
214 st->st_mtime != file->modtime) {
216 if (set_modtime(fname,file->modtime) != 0) {
217 fprintf(FERROR,"failed to set times on %s : %s\n",
218 fname,strerror(errno));
224 if (preserve_perms && !S_ISLNK(st->st_mode) &&
225 st->st_mode != file->mode) {
227 if (chmod(fname,file->mode) != 0) {
228 fprintf(FERROR,"failed to set permissions on %s : %s\n",
229 fname,strerror(errno));
235 if ((am_root && preserve_uid && st->st_uid != file->uid) ||
236 (preserve_gid && st->st_gid != file->gid)) {
239 (am_root&&preserve_uid)?file->uid:-1,
240 preserve_gid?file->gid:-1) != 0) {
241 if (verbose>1 || preserve_uid)
242 fprintf(FERROR,"chown %s : %s\n",fname,strerror(errno));
247 if (verbose > 1 && report) {
249 fprintf(FINFO,"%s\n",fname);
251 fprintf(FINFO,"%s is uptodate\n",fname);
257 void recv_generator(char *fname,struct file_list *flist,int i,int f_out)
261 struct map_struct *buf;
262 struct sum_struct *s;
263 char sum[MD4_SUM_LENGTH];
265 struct file_struct *file = &flist->files[i];
268 fprintf(FERROR,"recv_generator(%s,%d)\n",fname,i);
270 statret = lstat(fname,&st);
272 if (S_ISDIR(file->mode)) {
274 if (statret == 0 && !S_ISDIR(st.st_mode)) {
275 if (unlink(fname) != 0) {
276 fprintf(FERROR,"unlink %s : %s\n",fname,strerror(errno));
281 if (statret != 0 && mkdir(fname,file->mode) != 0 && errno != EEXIST) {
282 if (!(relative_paths && errno==ENOENT &&
283 create_directory_path(fname)==0 &&
284 mkdir(fname,file->mode)==0)) {
285 fprintf(FERROR,"mkdir %s : %s (2)\n",
286 fname,strerror(errno));
289 if (set_perms(fname,file,NULL,0) && verbose)
290 fprintf(FINFO,"%s/\n",fname);
295 if (preserve_links && S_ISLNK(file->mode)) {
296 char lnk[MAXPATHLEN];
299 l = readlink(fname,lnk,MAXPATHLEN-1);
302 if (strcmp(lnk,file->link) == 0) {
303 set_perms(fname,file,&st,1);
308 if (!dry_run) unlink(fname);
309 if (!dry_run && symlink(file->link,fname) != 0) {
310 fprintf(FERROR,"link %s -> %s : %s\n",
311 fname,file->link,strerror(errno));
313 set_perms(fname,file,NULL,0);
315 fprintf(FINFO,"%s -> %s\n",
323 if (am_root && preserve_devices && IS_DEVICE(file->mode)) {
325 st.st_mode != file->mode ||
326 st.st_rdev != file->rdev) {
327 if (!dry_run) unlink(fname);
329 fprintf(FERROR,"mknod(%s,0%o,0x%x)\n",
330 fname,(int)file->mode,(int)file->rdev);
332 mknod(fname,file->mode,file->rdev) != 0) {
333 fprintf(FERROR,"mknod %s : %s\n",fname,strerror(errno));
335 set_perms(fname,file,NULL,0);
337 fprintf(FINFO,"%s\n",fname);
340 set_perms(fname,file,&st,1);
346 if (preserve_hard_links && check_hard_link(file)) {
348 fprintf(FINFO,"%s is a hard link\n",file->name);
352 if (!S_ISREG(file->mode)) {
353 fprintf(FERROR,"skipping non-regular file %s\n",fname);
358 if (errno == ENOENT) {
360 if (!dry_run) send_sums(NULL,f_out);
363 fprintf(FERROR,"recv_generator failed to open %s\n",fname);
368 if (!S_ISREG(st.st_mode)) {
369 /* its not a regular file on the receiving end, but it is on the
370 sending end. If its a directory then skip it (too dangerous to
371 do a recursive deletion??) otherwise try to unlink it */
372 if (S_ISDIR(st.st_mode)) {
373 fprintf(FERROR,"ERROR: %s is a directory\n",fname);
376 if (unlink(fname) != 0) {
377 fprintf(FERROR,"%s : not a regular file (generator)\n",fname);
381 /* now pretend the file didn't exist */
383 if (!dry_run) send_sums(NULL,f_out);
387 if (update_only && st.st_mtime >= file->modtime) {
389 fprintf(FERROR,"%s is newer\n",fname);
393 if (always_checksum && S_ISREG(st.st_mode)) {
394 file_checksum(fname,sum,st.st_size);
397 if (st.st_size == file->length &&
398 ((!ignore_times && st.st_mtime == file->modtime) ||
399 (always_checksum && S_ISREG(st.st_mode) &&
400 memcmp(sum,file->sum,csum_length) == 0))) {
401 set_perms(fname,file,&st,1);
411 fd = open(fname,O_RDONLY);
414 fprintf(FERROR,"failed to open %s : %s\n",fname,strerror(errno));
418 if (st.st_size > 0) {
419 buf = map_file(fd,st.st_size);
425 fprintf(FERROR,"gen mapped %s of size %d\n",fname,(int)st.st_size);
427 s = generate_sums(buf,st.st_size,block_size);
430 fprintf(FERROR,"sending sums for %d\n",i);
437 if (buf) unmap_file(buf);
444 static int receive_data(int f_in,struct map_struct *buf,int fd,char *fname)
446 int i,n,remainder,len,count;
450 static char file_sum1[MD4_SUM_LENGTH];
451 static char file_sum2[MD4_SUM_LENGTH];
454 count = read_int(f_in);
456 remainder = read_int(f_in);
460 for (i=recv_token(f_in,&data); i != 0; i=recv_token(f_in,&data)) {
463 fprintf(FERROR,"data recv %d at %d\n",i,(int)offset);
467 if (fd != -1 && write_sparse(fd,data,i) != i) {
468 fprintf(FERROR,"write failed on %s : %s\n",fname,strerror(errno));
476 if (i == count-1 && remainder != 0)
480 fprintf(FERROR,"chunk[%d] of size %d at %d offset=%d\n",
481 i,len,(int)offset2,(int)offset);
483 map = map_ptr(buf,offset2,len);
488 if (fd != -1 && write_sparse(fd,map,len) != len) {
489 fprintf(FERROR,"write failed on %s : %s\n",fname,strerror(errno));
496 if (fd != -1 && offset > 0 && sparse_end(fd) != 0) {
497 fprintf(FERROR,"write failed on %s : %s\n",fname,strerror(errno));
503 if (remote_version >= 14) {
504 read_buf(f_in,file_sum2,MD4_SUM_LENGTH);
506 fprintf(FERROR,"got file_sum\n");
507 if (fd != -1 && memcmp(file_sum1,file_sum2,MD4_SUM_LENGTH) != 0)
514 static void delete_one(struct file_struct *f)
516 if (!S_ISDIR(f->mode)) {
517 if (!dry_run && unlink(f->name) != 0) {
518 fprintf(FERROR,"unlink %s : %s\n",f->name,strerror(errno));
519 } else if (verbose) {
520 fprintf(FERROR,"deleting %s\n",f->name);
523 if (!dry_run && rmdir(f->name) != 0) {
524 if (errno != ENOTEMPTY)
525 fprintf(FERROR,"rmdir %s : %s\n",f->name,strerror(errno));
526 } else if (verbose) {
527 fprintf(FERROR,"deleting directory %s\n",f->name);
533 static void delete_files(struct file_list *flist)
535 struct file_list *local_file_list;
542 if (!(local_file_list = send_file_list(-1,1,&dot)))
545 for (i=local_file_list->count-1;i>=0;i--) {
546 if (!local_file_list->files[i].name) continue;
547 if (-1 == flist_find(flist,&local_file_list->files[i])) {
548 delete_one(&local_file_list->files[i]);
553 static char *cleanup_fname = NULL;
555 void exit_cleanup(int code)
558 unlink(cleanup_fname);
568 int recv_files(int f_in,struct file_list *flist,char *local_name,int f_gen)
573 char fnametmp[MAXPATHLEN];
574 struct map_struct *buf;
576 struct file_struct *file;
581 fprintf(FERROR,"recv_files(%d) starting\n",flist->count);
584 if (recurse && delete_mode && !local_name && flist->count>0) {
592 if (phase==0 && remote_version >= 13) {
594 csum_length = SUM_LENGTH;
596 fprintf(FERROR,"recv_files phase=%d\n",phase);
604 file = &flist->files[i];
611 if (!am_server && verbose)
612 printf("%s\n",fname);
617 fprintf(FERROR,"recv_files(%s)\n",fname);
620 fd1 = open(fname,O_RDONLY);
622 if (fd1 != -1 && fstat(fd1,&st) != 0) {
623 fprintf(FERROR,"fstat %s : %s\n",fname,strerror(errno));
624 receive_data(f_in,NULL,-1,NULL);
629 if (fd1 != -1 && !S_ISREG(st.st_mode)) {
630 fprintf(FERROR,"%s : not a regular file (recv_files)\n",fname);
631 receive_data(f_in,NULL,-1,NULL);
636 if (fd1 != -1 && st.st_size > 0) {
637 buf = map_file(fd1,st.st_size);
639 fprintf(FERROR,"recv mapped %s of size %d\n",fname,(int)st.st_size);
645 sprintf(fnametmp,"%s.XXXXXX",fname);
646 if (NULL == mktemp(fnametmp)) {
647 fprintf(FERROR,"mktemp %s failed\n",fnametmp);
648 receive_data(f_in,buf,-1,NULL);
649 if (buf) unmap_file(buf);
653 fd2 = open(fnametmp,O_WRONLY|O_CREAT,file->mode);
654 if (relative_paths && errno == ENOENT &&
655 create_directory_path(fnametmp) == 0) {
656 fd2 = open(fnametmp,O_WRONLY|O_CREAT,file->mode);
659 fprintf(FERROR,"open %s : %s\n",fnametmp,strerror(errno));
660 receive_data(f_in,buf,-1,NULL);
661 if (buf) unmap_file(buf);
666 cleanup_fname = fnametmp;
668 if (!am_server && verbose)
669 printf("%s\n",fname);
672 recv_ok = receive_data(f_in,buf,fd2,fname);
675 if (buf) unmap_file(buf);
681 fprintf(FERROR,"renaming %s to %s\n",fnametmp,fname);
684 char fnamebak[MAXPATHLEN];
685 sprintf(fnamebak,"%s%s",fname,backup_suffix);
686 if (rename(fname,fnamebak) != 0 && errno != ENOENT) {
687 fprintf(FERROR,"rename %s %s : %s\n",fname,fnamebak,strerror(errno));
692 /* move tmp file over real file */
693 if (rename(fnametmp,fname) != 0) {
694 fprintf(FERROR,"rename %s -> %s : %s\n",
695 fnametmp,fname,strerror(errno));
699 cleanup_fname = NULL;
701 set_perms(fname,file,NULL,0);
705 fprintf(FERROR,"redoing %s(%d)\n",fname,i);
706 if (csum_length == SUM_LENGTH)
707 fprintf(FERROR,"ERROR: file corruption in %s\n",fname);
712 /* now we need to fix any directory permissions that were
713 modified during the transfer */
715 for (i = 0; i < flist->count; i++) {
716 struct file_struct *file = &flist->files[i];
717 if (!file->name || !S_ISDIR(file->mode)) continue;
718 recv_generator(file->name,flist,i,-1);
724 fprintf(FERROR,"recv_files finished\n");
731 off_t send_files(struct file_list *flist,int f_out,int f_in)
734 struct sum_struct *s;
735 struct map_struct *buf;
737 char fname[MAXPATHLEN];
740 struct file_struct *file;
744 fprintf(FERROR,"send_files starting\n");
746 setup_nonblocking(f_in,f_out);
752 if (phase==0 && remote_version >= 13) {
754 csum_length = SUM_LENGTH;
758 fprintf(FERROR,"send_files phase=%d\n",phase);
764 file = &flist->files[i];
768 strcpy(fname,file->dir);
771 strcat(fname,file->name);
774 fprintf(FERROR,"send_files(%d,%s)\n",i,fname);
777 if (!am_server && verbose)
778 printf("%s\n",fname);
783 s = receive_sums(f_in);
785 fprintf(FERROR,"receive_sums failed\n");
789 fd = open(fname,O_RDONLY);
791 fprintf(FERROR,"send_files failed to open %s: %s\n",
792 fname,strerror(errno));
796 /* map the local file */
797 if (fstat(fd,&st) != 0) {
798 fprintf(FERROR,"fstat failed : %s\n",strerror(errno));
802 if (st.st_size > 0) {
803 buf = map_file(fd,st.st_size);
809 fprintf(FERROR,"send_files mapped %s of size %d\n",
810 fname,(int)st.st_size);
814 write_int(f_out,s->count);
815 write_int(f_out,s->n);
816 write_int(f_out,s->remainder);
819 fprintf(FERROR,"calling match_sums %s\n",fname);
821 if (!am_server && verbose)
822 printf("%s\n",fname);
824 match_sums(f_out,s,buf,st.st_size);
827 if (buf) unmap_file(buf);
833 fprintf(FERROR,"sender finished %s\n",fname);
839 fprintf(FERROR,"send files finished\n");
851 void generate_files(int f,struct file_list *flist,char *local_name,int f_recv)
857 fprintf(FERROR,"generator starting pid=%d count=%d\n",
858 (int)getpid(),flist->count);
860 for (i = 0; i < flist->count; i++) {
861 struct file_struct *file = &flist->files[i];
862 mode_t saved_mode = file->mode;
863 if (!file->name) continue;
865 /* we need to ensure that any directories we create have writeable
866 permissions initially so that we can create the files within
867 them. This is then fixed after the files are transferred */
868 if (!am_root && S_ISDIR(file->mode)) {
869 file->mode |= S_IWUSR; /* user write */
872 recv_generator(local_name?local_name:file->name,
875 file->mode = saved_mode;
879 csum_length = SUM_LENGTH;
883 fprintf(FERROR,"generate_files phase=%d\n",phase);
888 if (remote_version >= 13) {
889 /* in newer versions of the protocol the files can cycle through
890 the system more than once to catch initial checksum errors */
891 for (i=read_int(f_recv); i != -1; i=read_int(f_recv)) {
892 struct file_struct *file = &flist->files[i];
893 recv_generator(local_name?local_name:file->name,
899 fprintf(FERROR,"generate_files phase=%d\n",phase);
907 fprintf(FERROR,"generator wrote %d\n",write_total());