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;
52 static void free_sums(struct sum_struct *s)
54 if (s->sums) free(s->sums);
61 send a sums struct down a fd
63 static void send_sums(struct sum_struct *s,int f_out)
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);
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);
82 generate a stream of signatures/checksums that describe a buffer
84 generate approximately one checksum every n bytes
86 static struct sum_struct *generate_sums(struct map_struct *buf,off_t len,int n)
92 int remainder = (len%block_len);
95 count = (len+(block_len-1))/block_len;
97 s = (struct sum_struct *)malloc(sizeof(*s));
98 if (!s) out_of_memory("generate_sums");
101 s->remainder = remainder;
111 fprintf(FERROR,"count=%d rem=%d n=%d flength=%d\n",
112 s->count,s->remainder,s->n,(int)s->flength);
114 s->sums = (struct sum_buf *)malloc(sizeof(s->sums[0])*s->count);
115 if (!s->sums) out_of_memory("generate_sums");
117 for (i=0;i<count;i++) {
119 char *map = map_ptr(buf,offset,n1);
121 s->sums[i].sum1 = get_checksum1(map,n1);
122 get_checksum2(map,n1,s->sums[i].sum2);
124 s->sums[i].offset = offset;
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);
141 receive the checksums for a buffer
143 static struct sum_struct *receive_sums(int f)
145 struct sum_struct *s;
150 s = (struct sum_struct *)malloc(sizeof(*s));
151 if (!s) out_of_memory("receive_sums");
153 s->count = read_int(f);
155 s->remainder = read_int(f);
159 fprintf(FERROR,"count=%d n=%d rem=%d\n",
160 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 void set_perms(char *fname,struct file_struct *file,struct stat *st,
204 if (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 ((preserve_uid && st->st_uid != file->uid) ||
234 (preserve_gid && st->st_gid != file->gid)) {
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));
245 if (verbose > 1 && report) {
247 fprintf(FINFO,"%s\n",fname);
249 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[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);
270 if (preserve_links && S_ISLNK(file->mode)) {
271 char lnk[MAXPATHLEN];
274 l = readlink(fname,lnk,MAXPATHLEN-1);
277 if (strcmp(lnk,file->link) == 0) {
278 set_perms(fname,file,&st,1);
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));
288 set_perms(fname,file,NULL,0);
290 fprintf(FINFO,"%s -> %s\n",
298 if (preserve_devices && IS_DEVICE(file->mode)) {
300 st.st_mode != file->mode ||
301 st.st_rdev != file->rdev) {
302 if (!dry_run) unlink(fname);
304 fprintf(FERROR,"mknod(%s,0%o,0x%x)\n",
305 fname,(int)file->mode,(int)file->rdev);
307 mknod(fname,file->mode,file->rdev) != 0) {
308 fprintf(FERROR,"mknod %s : %s\n",fname,strerror(errno));
310 set_perms(fname,file,NULL,0);
312 fprintf(FINFO,"%s\n",fname);
315 set_perms(fname,file,&st,1);
321 if (preserve_hard_links && check_hard_link(file)) {
323 fprintf(FINFO,"%s is a hard link\n",file->name);
327 if (!S_ISREG(file->mode)) {
328 fprintf(FERROR,"skipping non-regular file %s\n",fname);
333 if (errno == ENOENT) {
335 if (!dry_run) send_sums(NULL,f_out);
338 fprintf(FERROR,"recv_generator failed to open %s\n",fname);
343 if (!S_ISREG(st.st_mode)) {
344 fprintf(FERROR,"%s : not a regular file\n",fname);
348 if (update_only && st.st_mtime >= file->modtime) {
350 fprintf(FERROR,"%s is newer\n",fname);
354 if (always_checksum && S_ISREG(st.st_mode)) {
355 file_checksum(fname,sum,st.st_size);
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);
372 fd = open(fname,O_RDONLY);
375 fprintf(FERROR,"failed to open %s : %s\n",fname,strerror(errno));
379 if (st.st_size > 0) {
380 buf = map_file(fd,st.st_size);
386 fprintf(FERROR,"gen mapped %s of size %d\n",fname,(int)st.st_size);
388 s = generate_sums(buf,st.st_size,block_size);
391 fprintf(FERROR,"sending sums for %d\n",i);
398 if (buf) unmap_file(buf);
405 static int receive_data(int f_in,struct map_struct *buf,int fd,char *fname)
407 int i,n,remainder,len,count;
411 static char file_sum1[SUM_LENGTH];
412 static char file_sum2[SUM_LENGTH];
415 count = read_int(f_in);
417 remainder = read_int(f_in);
421 for (i=recv_token(f_in,&data); i != 0; i=recv_token(f_in,&data)) {
424 fprintf(FERROR,"data recv %d at %d\n",i,(int)offset);
428 if (write_sparse(fd,data,i) != i) {
429 fprintf(FERROR,"write failed on %s : %s\n",fname,strerror(errno));
437 if (i == count-1 && remainder != 0)
441 fprintf(FERROR,"chunk[%d] of size %d at %d offset=%d\n",
442 i,len,(int)offset2,(int)offset);
444 map = map_ptr(buf,offset2,len);
449 if (write_sparse(fd,map,len) != len) {
450 fprintf(FERROR,"write failed on %s : %s\n",fname,strerror(errno));
457 if (offset > 0 && sparse_end(fd) != 0) {
458 fprintf(FERROR,"write failed on %s : %s\n",fname,strerror(errno));
464 if (remote_version >= 14) {
465 read_buf(f_in,file_sum2,SUM_LENGTH);
467 fprintf(FERROR,"got file_sum\n");
468 if (memcmp(file_sum1,file_sum2,SUM_LENGTH) != 0)
475 static void delete_one(struct file_struct *f)
477 if (!S_ISDIR(f->mode)) {
478 if (!dry_run && unlink(f->name) != 0) {
479 fprintf(FERROR,"unlink %s : %s\n",f->name,strerror(errno));
480 } else if (verbose) {
481 fprintf(FERROR,"deleting %s\n",f->name);
484 if (!dry_run && rmdir(f->name) != 0) {
485 if (errno != ENOTEMPTY)
486 fprintf(FERROR,"rmdir %s : %s\n",f->name,strerror(errno));
487 } else if (verbose) {
488 fprintf(FERROR,"deleting directory %s\n",f->name);
494 static void delete_files(struct file_list *flist)
496 struct file_list *local_file_list;
503 if (!(local_file_list = send_file_list(-1,recurse,1,&dot)))
506 for (i=local_file_list->count;i>=0;i--) {
507 if (!local_file_list->files[i].name) continue;
508 if (-1 == flist_find(flist,&local_file_list->files[i])) {
509 delete_one(&local_file_list->files[i]);
514 static char *cleanup_fname = NULL;
516 void exit_cleanup(int code)
519 unlink(cleanup_fname);
529 int recv_files(int f_in,struct file_list *flist,char *local_name,int f_gen)
534 char fnametmp[MAXPATHLEN];
535 struct map_struct *buf;
537 struct file_struct *file;
542 fprintf(FERROR,"recv_files(%d) starting\n",flist->count);
545 if (recurse && delete_mode && !local_name && flist->count>0) {
553 if (phase==0 && remote_version >= 13) {
555 csum_length = SUM_LENGTH;
557 fprintf(FERROR,"recv_files phase=%d\n",phase);
565 file = &flist->files[i];
572 if (!am_server && verbose)
573 printf("%s\n",fname);
578 fprintf(FERROR,"recv_files(%s)\n",fname);
581 fd1 = open(fname,O_RDONLY);
583 if (fd1 != -1 && fstat(fd1,&st) != 0) {
584 fprintf(FERROR,"fstat %s : %s\n",fname,strerror(errno));
589 if (fd1 != -1 && !S_ISREG(st.st_mode)) {
590 fprintf(FERROR,"%s : not a regular file\n",fname);
595 if (fd1 != -1 && st.st_size > 0) {
596 buf = map_file(fd1,st.st_size);
598 fprintf(FERROR,"recv mapped %s of size %d\n",fname,(int)st.st_size);
604 sprintf(fnametmp,"%s.XXXXXX",fname);
605 if (NULL == mktemp(fnametmp)) {
606 fprintf(FERROR,"mktemp %s failed\n",fnametmp);
609 fd2 = open(fnametmp,O_WRONLY|O_CREAT,file->mode);
611 fprintf(FERROR,"open %s : %s\n",fnametmp,strerror(errno));
615 cleanup_fname = fnametmp;
617 if (!am_server && verbose)
618 printf("%s\n",fname);
621 recv_ok = receive_data(f_in,buf,fd2,fname);
624 if (buf) unmap_file(buf);
630 fprintf(FERROR,"renaming %s to %s\n",fnametmp,fname);
633 char fnamebak[MAXPATHLEN];
634 sprintf(fnamebak,"%s%s",fname,backup_suffix);
635 if (rename(fname,fnamebak) != 0 && errno != ENOENT) {
636 fprintf(FERROR,"rename %s %s : %s\n",fname,fnamebak,strerror(errno));
641 /* move tmp file over real file */
642 if (rename(fnametmp,fname) != 0) {
643 fprintf(FERROR,"rename %s -> %s : %s\n",
644 fnametmp,fname,strerror(errno));
647 cleanup_fname = NULL;
649 set_perms(fname,file,NULL,0);
653 fprintf(FERROR,"redoing %s(%d)\n",fname,i);
659 fprintf(FERROR,"recv_files finished\n");
666 off_t send_files(struct file_list *flist,int f_out,int f_in)
669 struct sum_struct *s;
670 struct map_struct *buf;
672 char fname[MAXPATHLEN];
675 struct file_struct *file;
679 fprintf(FERROR,"send_files starting\n");
681 setup_nonblocking(f_in,f_out);
687 if (phase==0 && remote_version >= 13) {
689 csum_length = SUM_LENGTH;
693 fprintf(FERROR,"send_files phase=%d\n",phase);
699 file = &flist->files[i];
703 strcpy(fname,file->dir);
706 strcat(fname,file->name);
709 fprintf(FERROR,"send_files(%d,%s)\n",i,fname);
712 if (!am_server && verbose)
713 printf("%s\n",fname);
718 s = receive_sums(f_in);
720 fprintf(FERROR,"receive_sums failed\n");
724 fd = open(fname,O_RDONLY);
726 fprintf(FERROR,"send_files failed to open %s: %s\n",
727 fname,strerror(errno));
731 /* map the local file */
732 if (fstat(fd,&st) != 0) {
733 fprintf(FERROR,"fstat failed : %s\n",strerror(errno));
737 if (st.st_size > 0) {
738 buf = map_file(fd,st.st_size);
744 fprintf(FERROR,"send_files mapped %s of size %d\n",
745 fname,(int)st.st_size);
749 write_int(f_out,s->count);
750 write_int(f_out,s->n);
751 write_int(f_out,s->remainder);
754 fprintf(FERROR,"calling match_sums %s\n",fname);
756 if (!am_server && verbose)
757 printf("%s\n",fname);
759 match_sums(f_out,s,buf,st.st_size);
762 if (buf) unmap_file(buf);
768 fprintf(FERROR,"sender finished %s\n",fname);
774 fprintf(FERROR,"send files finished\n");
786 void generate_files(int f,struct file_list *flist,char *local_name,int f_recv)
792 fprintf(FERROR,"generator starting pid=%d count=%d\n",
793 (int)getpid(),flist->count);
795 for (i = 0; i < flist->count; i++) {
796 struct file_struct *file = &flist->files[i];
797 if (!file->name) continue;
798 if (S_ISDIR(file->mode)) {
799 if (dry_run) continue;
800 if (mkdir(file->name,file->mode) != 0 &&
802 fprintf(FERROR,"mkdir %s : %s\n",
803 file->name,strerror(errno));
807 recv_generator(local_name?local_name:file->name,
812 csum_length = SUM_LENGTH;
816 fprintf(FERROR,"generate_files phase=%d\n",phase);
821 if (remote_version >= 13) {
822 for (i=read_int(f_recv); i != -1; i=read_int(f_recv)) {
823 struct file_struct *file = &flist->files[i];
824 recv_generator(local_name?local_name:file->name,
830 fprintf(FERROR,"generate_files phase=%d\n",phase);
838 fprintf(FERROR,"generator wrote %d\n",write_total());