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;
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);
166 s->sums = (struct sum_buf *)malloc(sizeof(s->sums[0])*s->count);
167 if (!s->sums) out_of_memory("receive_sums");
169 for (i=0;i<s->count;i++) {
170 s->sums[i].sum1 = read_int(f);
171 read_buf(f,s->sums[i].sum2,csum_length);
173 s->sums[i].offset = offset;
176 if (i == s->count-1 && s->remainder != 0) {
177 s->sums[i].len = s->remainder;
179 s->sums[i].len = s->n;
181 offset += s->sums[i].len;
184 fprintf(FERROR,"chunk[%d] len=%d offset=%d sum1=%08x\n",
185 i,s->sums[i].len,(int)s->sums[i].offset,s->sums[i].sum1);
194 static int set_perms(char *fname,struct file_struct *file,struct stat *st,
200 if (dry_run) return 0;
203 if (lstat(fname,&st2) != 0) {
204 fprintf(FERROR,"stat %s : %s\n",fname,strerror(errno));
210 if (preserve_times && !S_ISLNK(st->st_mode) &&
211 st->st_mtime != file->modtime) {
213 if (set_modtime(fname,file->modtime) != 0) {
214 fprintf(FERROR,"failed to set times on %s : %s\n",
215 fname,strerror(errno));
221 if (preserve_perms && !S_ISLNK(st->st_mode) &&
222 st->st_mode != file->mode) {
224 if (chmod(fname,file->mode) != 0) {
225 fprintf(FERROR,"failed to set permissions on %s : %s\n",
226 fname,strerror(errno));
232 if ((am_root && preserve_uid && st->st_uid != file->uid) ||
233 (preserve_gid && st->st_gid != file->gid)) {
236 (am_root&&preserve_uid)?file->uid:-1,
237 preserve_gid?file->gid:-1) != 0) {
238 if (verbose>1 || preserve_uid)
239 fprintf(FERROR,"chown %s : %s\n",fname,strerror(errno));
244 if (verbose > 1 && report) {
246 fprintf(FINFO,"%s\n",fname);
248 fprintf(FINFO,"%s is uptodate\n",fname);
254 void recv_generator(char *fname,struct file_list *flist,int i,int f_out)
258 struct map_struct *buf;
259 struct sum_struct *s;
260 char sum[MD4_SUM_LENGTH];
262 struct file_struct *file = &flist->files[i];
265 fprintf(FERROR,"recv_generator(%s,%d)\n",fname,i);
267 statret = lstat(fname,&st);
269 if (S_ISDIR(file->mode)) {
271 if (statret == 0 && !S_ISDIR(st.st_mode)) {
272 if (unlink(fname) != 0) {
273 fprintf(FERROR,"unlink %s : %s\n",fname,strerror(errno));
278 if (statret != 0 && mkdir(fname,file->mode) != 0 && errno != EEXIST) {
279 if (!(relative_paths && errno==ENOENT &&
280 create_directory_path(fname)==0 &&
281 mkdir(fname,file->mode)==0)) {
282 fprintf(FERROR,"mkdir %s : %s (2)\n",
283 fname,strerror(errno));
286 if (set_perms(fname,file,NULL,0) && verbose)
287 fprintf(FINFO,"%s/\n",fname);
292 if (preserve_links && S_ISLNK(file->mode)) {
293 char lnk[MAXPATHLEN];
296 l = readlink(fname,lnk,MAXPATHLEN-1);
299 if (strcmp(lnk,file->link) == 0) {
300 set_perms(fname,file,&st,1);
305 if (!dry_run) unlink(fname);
306 if (!dry_run && symlink(file->link,fname) != 0) {
307 fprintf(FERROR,"link %s -> %s : %s\n",
308 fname,file->link,strerror(errno));
310 set_perms(fname,file,NULL,0);
312 fprintf(FINFO,"%s -> %s\n",
320 if (am_root && preserve_devices && IS_DEVICE(file->mode)) {
322 st.st_mode != file->mode ||
323 st.st_rdev != file->rdev) {
324 if (!dry_run) unlink(fname);
326 fprintf(FERROR,"mknod(%s,0%o,0x%x)\n",
327 fname,(int)file->mode,(int)file->rdev);
329 mknod(fname,file->mode,file->rdev) != 0) {
330 fprintf(FERROR,"mknod %s : %s\n",fname,strerror(errno));
332 set_perms(fname,file,NULL,0);
334 fprintf(FINFO,"%s\n",fname);
337 set_perms(fname,file,&st,1);
343 if (preserve_hard_links && check_hard_link(file)) {
345 fprintf(FINFO,"%s is a hard link\n",file->name);
349 if (!S_ISREG(file->mode)) {
350 fprintf(FERROR,"skipping non-regular file %s\n",fname);
355 if (errno == ENOENT) {
357 if (!dry_run) send_sums(NULL,f_out);
360 fprintf(FERROR,"recv_generator failed to open %s\n",fname);
365 if (!S_ISREG(st.st_mode)) {
366 /* its not a regular file on the receiving end, but it is on the
367 sending end. If its a directory then skip it (too dangerous to
368 do a recursive deletion??) otherwise try to unlink it */
369 if (S_ISDIR(st.st_mode)) {
370 fprintf(FERROR,"ERROR: %s is a directory\n",fname);
373 if (unlink(fname) != 0) {
374 fprintf(FERROR,"%s : not a regular file (generator)\n",fname);
378 /* now pretend the file didn't exist */
380 if (!dry_run) send_sums(NULL,f_out);
384 if (update_only && st.st_mtime >= file->modtime) {
386 fprintf(FERROR,"%s is newer\n",fname);
390 if (always_checksum && S_ISREG(st.st_mode)) {
391 file_checksum(fname,sum,st.st_size);
394 if (st.st_size == file->length &&
395 ((!ignore_times && st.st_mtime == file->modtime) ||
396 (always_checksum && S_ISREG(st.st_mode) &&
397 memcmp(sum,file->sum,csum_length) == 0))) {
398 set_perms(fname,file,&st,1);
408 fd = open(fname,O_RDONLY);
411 fprintf(FERROR,"failed to open %s : %s\n",fname,strerror(errno));
415 if (st.st_size > 0) {
416 buf = map_file(fd,st.st_size);
422 fprintf(FERROR,"gen mapped %s of size %d\n",fname,(int)st.st_size);
424 s = generate_sums(buf,st.st_size,block_size);
427 fprintf(FERROR,"sending sums for %d\n",i);
434 if (buf) unmap_file(buf);
441 static int receive_data(int f_in,struct map_struct *buf,int fd,char *fname)
443 int i,n,remainder,len,count;
447 static char file_sum1[MD4_SUM_LENGTH];
448 static char file_sum2[MD4_SUM_LENGTH];
451 count = read_int(f_in);
453 remainder = read_int(f_in);
457 for (i=recv_token(f_in,&data); i != 0; i=recv_token(f_in,&data)) {
460 fprintf(FERROR,"data recv %d at %d\n",i,(int)offset);
464 if (fd != -1 && write_sparse(fd,data,i) != i) {
465 fprintf(FERROR,"write failed on %s : %s\n",fname,strerror(errno));
473 if (i == count-1 && remainder != 0)
477 fprintf(FERROR,"chunk[%d] of size %d at %d offset=%d\n",
478 i,len,(int)offset2,(int)offset);
480 map = map_ptr(buf,offset2,len);
485 if (fd != -1 && write_sparse(fd,map,len) != len) {
486 fprintf(FERROR,"write failed on %s : %s\n",fname,strerror(errno));
493 if (fd != -1 && offset > 0 && sparse_end(fd) != 0) {
494 fprintf(FERROR,"write failed on %s : %s\n",fname,strerror(errno));
500 if (remote_version >= 14) {
501 read_buf(f_in,file_sum2,MD4_SUM_LENGTH);
503 fprintf(FERROR,"got file_sum\n");
504 if (fd != -1 && memcmp(file_sum1,file_sum2,MD4_SUM_LENGTH) != 0)
511 static void delete_one(struct file_struct *f)
513 if (!S_ISDIR(f->mode)) {
514 if (!dry_run && unlink(f->name) != 0) {
515 fprintf(FERROR,"unlink %s : %s\n",f->name,strerror(errno));
516 } else if (verbose) {
517 fprintf(FERROR,"deleting %s\n",f->name);
520 if (!dry_run && rmdir(f->name) != 0) {
521 if (errno != ENOTEMPTY)
522 fprintf(FERROR,"rmdir %s : %s\n",f->name,strerror(errno));
523 } else if (verbose) {
524 fprintf(FERROR,"deleting directory %s\n",f->name);
530 static void delete_files(struct file_list *flist)
532 struct file_list *local_file_list;
539 if (!(local_file_list = send_file_list(-1,1,&dot)))
542 for (i=local_file_list->count-1;i>=0;i--) {
543 if (!local_file_list->files[i].name) continue;
544 if (-1 == flist_find(flist,&local_file_list->files[i])) {
545 delete_one(&local_file_list->files[i]);
550 static char *cleanup_fname = NULL;
552 void exit_cleanup(int code)
555 unlink(cleanup_fname);
565 int recv_files(int f_in,struct file_list *flist,char *local_name,int f_gen)
570 char fnametmp[MAXPATHLEN];
571 struct map_struct *buf;
573 struct file_struct *file;
578 fprintf(FERROR,"recv_files(%d) starting\n",flist->count);
581 if (recurse && delete_mode && !local_name && flist->count>0) {
589 if (phase==0 && remote_version >= 13) {
591 csum_length = SUM_LENGTH;
593 fprintf(FERROR,"recv_files phase=%d\n",phase);
601 file = &flist->files[i];
608 if (!am_server && verbose)
609 printf("%s\n",fname);
614 fprintf(FERROR,"recv_files(%s)\n",fname);
617 fd1 = open(fname,O_RDONLY);
619 if (fd1 != -1 && fstat(fd1,&st) != 0) {
620 fprintf(FERROR,"fstat %s : %s\n",fname,strerror(errno));
621 receive_data(f_in,NULL,-1,NULL);
626 if (fd1 != -1 && !S_ISREG(st.st_mode)) {
627 fprintf(FERROR,"%s : not a regular file (recv_files)\n",fname);
628 receive_data(f_in,NULL,-1,NULL);
633 if (fd1 != -1 && st.st_size > 0) {
634 buf = map_file(fd1,st.st_size);
636 fprintf(FERROR,"recv mapped %s of size %d\n",fname,(int)st.st_size);
642 sprintf(fnametmp,"%s.XXXXXX",fname);
643 if (NULL == mktemp(fnametmp)) {
644 fprintf(FERROR,"mktemp %s failed\n",fnametmp);
645 receive_data(f_in,buf,-1,NULL);
646 if (buf) unmap_file(buf);
650 fd2 = open(fnametmp,O_WRONLY|O_CREAT,file->mode);
651 if (relative_paths && errno == ENOENT &&
652 create_directory_path(fnametmp) == 0) {
653 fd2 = open(fnametmp,O_WRONLY|O_CREAT,file->mode);
656 fprintf(FERROR,"open %s : %s\n",fnametmp,strerror(errno));
657 receive_data(f_in,buf,-1,NULL);
658 if (buf) unmap_file(buf);
663 cleanup_fname = fnametmp;
665 if (!am_server && verbose)
666 printf("%s\n",fname);
669 recv_ok = receive_data(f_in,buf,fd2,fname);
672 if (buf) unmap_file(buf);
678 fprintf(FERROR,"renaming %s to %s\n",fnametmp,fname);
681 char fnamebak[MAXPATHLEN];
682 sprintf(fnamebak,"%s%s",fname,backup_suffix);
683 if (rename(fname,fnamebak) != 0 && errno != ENOENT) {
684 fprintf(FERROR,"rename %s %s : %s\n",fname,fnamebak,strerror(errno));
689 /* move tmp file over real file */
690 if (rename(fnametmp,fname) != 0) {
691 fprintf(FERROR,"rename %s -> %s : %s\n",
692 fnametmp,fname,strerror(errno));
696 cleanup_fname = NULL;
698 set_perms(fname,file,NULL,0);
702 fprintf(FERROR,"redoing %s(%d)\n",fname,i);
703 if (csum_length == SUM_LENGTH)
704 fprintf(FERROR,"ERROR: file corruption in %s\n",fname);
709 /* now we need to fix any directory permissions that were
710 modified during the transfer */
712 for (i = 0; i < flist->count; i++) {
713 struct file_struct *file = &flist->files[i];
714 if (!file->name || !S_ISDIR(file->mode)) continue;
715 recv_generator(file->name,flist,i,-1);
721 fprintf(FERROR,"recv_files finished\n");
728 off_t send_files(struct file_list *flist,int f_out,int f_in)
731 struct sum_struct *s;
732 struct map_struct *buf;
734 char fname[MAXPATHLEN];
737 struct file_struct *file;
741 fprintf(FERROR,"send_files starting\n");
743 setup_nonblocking(f_in,f_out);
749 if (phase==0 && remote_version >= 13) {
751 csum_length = SUM_LENGTH;
755 fprintf(FERROR,"send_files phase=%d\n",phase);
761 file = &flist->files[i];
765 strcpy(fname,file->dir);
768 strcat(fname,file->name);
771 fprintf(FERROR,"send_files(%d,%s)\n",i,fname);
774 if (!am_server && verbose)
775 printf("%s\n",fname);
780 s = receive_sums(f_in);
782 fprintf(FERROR,"receive_sums failed\n");
786 fd = open(fname,O_RDONLY);
788 fprintf(FERROR,"send_files failed to open %s: %s\n",
789 fname,strerror(errno));
793 /* map the local file */
794 if (fstat(fd,&st) != 0) {
795 fprintf(FERROR,"fstat failed : %s\n",strerror(errno));
799 if (st.st_size > 0) {
800 buf = map_file(fd,st.st_size);
806 fprintf(FERROR,"send_files mapped %s of size %d\n",
807 fname,(int)st.st_size);
811 write_int(f_out,s->count);
812 write_int(f_out,s->n);
813 write_int(f_out,s->remainder);
816 fprintf(FERROR,"calling match_sums %s\n",fname);
818 if (!am_server && verbose)
819 printf("%s\n",fname);
821 match_sums(f_out,s,buf,st.st_size);
824 if (buf) unmap_file(buf);
830 fprintf(FERROR,"sender finished %s\n",fname);
836 fprintf(FERROR,"send files finished\n");
848 void generate_files(int f,struct file_list *flist,char *local_name,int f_recv)
854 fprintf(FERROR,"generator starting pid=%d count=%d\n",
855 (int)getpid(),flist->count);
857 for (i = 0; i < flist->count; i++) {
858 struct file_struct *file = &flist->files[i];
859 mode_t saved_mode = file->mode;
860 if (!file->name) continue;
862 /* we need to ensure that any directories we create have writeable
863 permissions initially so that we can create the files within
864 them. This is then fixed after the files are transferred */
865 if (!am_root && S_ISDIR(file->mode)) {
866 file->mode |= S_IWUSR; /* user write */
869 recv_generator(local_name?local_name:file->name,
872 file->mode = saved_mode;
876 csum_length = SUM_LENGTH;
880 fprintf(FERROR,"generate_files phase=%d\n",phase);
885 if (remote_version >= 13) {
886 /* in newer versions of the protocol the files can cycle through
887 the system more than once to catch initial checksum errors */
888 for (i=read_int(f_recv); i != -1; i=read_int(f_recv)) {
889 struct file_struct *file = &flist->files[i];
890 recv_generator(local_name?local_name:file->name,
896 fprintf(FERROR,"generate_files phase=%d\n",phase);
904 fprintf(FERROR,"generator wrote %d\n",write_total());