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 char *backup_suffix;
31 extern int block_size;
32 extern int update_only;
33 extern int make_backups;
34 extern int preserve_links;
35 extern int preserve_perms;
36 extern int preserve_devices;
37 extern int preserve_uid;
38 extern int preserve_gid;
39 extern int preserve_times;
41 extern int ignore_times;
43 extern int delete_mode;
44 extern int cvs_exclude;
49 static void free_sums(struct sum_struct *s)
51 if (s->sums) free(s->sums);
58 send a sums struct down a fd
60 static void send_sums(struct sum_struct *s,int f_out)
64 /* tell the other guy how many we are going to be doing and how many
65 bytes there are in the last chunk */
66 write_int(f_out,s?s->count:0);
67 write_int(f_out,s?s->n:block_size);
68 write_int(f_out,s?s->remainder:0);
70 for (i=0;i<s->count;i++) {
71 write_int(f_out,s->sums[i].sum1);
72 write_buf(f_out,s->sums[i].sum2,csum_length);
79 generate a stream of signatures/checksums that describe a buffer
81 generate approximately one checksum every n bytes
83 static struct sum_struct *generate_sums(char *buf,off_t len,int n)
89 int remainder = (len%block_len);
92 count = (len+(block_len-1))/block_len;
94 s = (struct sum_struct *)malloc(sizeof(*s));
95 if (!s) out_of_memory("generate_sums");
98 s->remainder = remainder;
108 fprintf(stderr,"count=%d rem=%d n=%d flength=%d\n",
109 s->count,s->remainder,s->n,(int)s->flength);
111 s->sums = (struct sum_buf *)malloc(sizeof(s->sums[0])*s->count);
112 if (!s->sums) out_of_memory("generate_sums");
114 for (i=0;i<count;i++) {
116 char *map = map_ptr(buf,offset,n1);
118 s->sums[i].sum1 = get_checksum1(map,n1);
119 get_checksum2(map,n1,s->sums[i].sum2);
121 s->sums[i].offset = offset;
126 fprintf(stderr,"chunk[%d] offset=%d len=%d sum1=%08x\n",
127 i,(int)s->sums[i].offset,s->sums[i].len,s->sums[i].sum1);
138 receive the checksums for a buffer
140 static struct sum_struct *receive_sums(int f)
142 struct sum_struct *s;
147 s = (struct sum_struct *)malloc(sizeof(*s));
148 if (!s) out_of_memory("receive_sums");
150 s->count = read_int(f);
152 s->remainder = read_int(f);
156 fprintf(stderr,"count=%d n=%d rem=%d\n",
157 s->count,s->n,s->remainder);
164 s->sums = (struct sum_buf *)malloc(sizeof(s->sums[0])*s->count);
165 if (!s->sums) out_of_memory("receive_sums");
167 for (i=0;i<s->count;i++) {
168 s->sums[i].sum1 = read_int(f);
169 read_buf(f,s->sums[i].sum2,csum_length);
171 s->sums[i].offset = offset;
174 if (i == s->count-1 && s->remainder != 0) {
175 s->sums[i].len = s->remainder;
177 s->sums[i].len = s->n;
179 offset += s->sums[i].len;
182 fprintf(stderr,"chunk[%d] len=%d offset=%d sum1=%08x\n",
183 i,s->sums[i].len,(int)s->sums[i].offset,s->sums[i].sum1);
192 static void set_perms(char *fname,struct file_struct *file,struct stat *st,
201 if (stat(fname,&st2) != 0) {
202 fprintf(stderr,"stat %s : %s\n",fname,strerror(errno));
208 if (preserve_times && st->st_mtime != file->modtime) {
210 if (set_modtime(fname,file->modtime) != 0) {
211 fprintf(stderr,"failed to set times on %s : %s\n",
212 fname,strerror(errno));
218 if (preserve_perms && st->st_mode != file->mode) {
220 if (chmod(fname,file->mode) != 0) {
221 fprintf(stderr,"failed to set permissions on %s : %s\n",
222 fname,strerror(errno));
228 if ((preserve_uid && st->st_uid != file->uid) ||
229 (preserve_gid && st->st_gid != file->gid)) {
232 preserve_uid?file->uid:-1,
233 preserve_gid?file->gid:-1) != 0) {
234 if (verbose>1 || preserve_uid)
235 fprintf(stderr,"chown %s : %s\n",fname,strerror(errno));
240 if (verbose > 1 && report) {
242 fprintf(am_server?stderr:stdout,"%s\n",fname);
244 fprintf(am_server?stderr:stdout,"%s is uptodate\n",fname);
249 void recv_generator(char *fname,struct file_list *flist,int i,int f_out)
254 struct sum_struct *s;
255 char sum[SUM_LENGTH];
259 fprintf(stderr,"recv_generator(%s)\n",fname);
261 statret = lstat(fname,&st);
264 if (preserve_links && S_ISLNK(flist->files[i].mode)) {
265 char lnk[MAXPATHLEN];
268 l = readlink(fname,lnk,MAXPATHLEN-1);
271 if (strcmp(lnk,flist->files[i].link) == 0) {
273 fprintf(am_server?stderr:stdout,"%s is uptodate\n",fname);
278 if (!dry_run) unlink(fname);
279 if (!dry_run && symlink(flist->files[i].link,fname) != 0) {
280 fprintf(stderr,"link %s -> %s : %s\n",
281 fname,flist->files[i].link,strerror(errno));
284 fprintf(am_server?stderr:stdout,"%s -> %s\n",fname,flist->files[i].link);
291 if (preserve_devices && IS_DEVICE(flist->files[i].mode)) {
293 st.st_mode != flist->files[i].mode ||
294 st.st_rdev != flist->files[i].dev) {
295 if (!dry_run) unlink(fname);
297 fprintf(stderr,"mknod(%s,0%o,0x%x)\n",
298 fname,(int)flist->files[i].mode,(int)flist->files[i].dev);
300 mknod(fname,flist->files[i].mode,flist->files[i].dev) != 0) {
301 fprintf(stderr,"mknod %s : %s\n",fname,strerror(errno));
303 set_perms(fname,&flist->files[i],NULL,0);
305 fprintf(am_server?stderr:stdout,"%s\n",fname);
308 set_perms(fname,&flist->files[i],&st,1);
314 if (!S_ISREG(flist->files[i].mode)) {
315 fprintf(stderr,"skipping non-regular file %s\n",fname);
320 if (errno == ENOENT) {
322 if (!dry_run) send_sums(NULL,f_out);
325 fprintf(stderr,"recv_generator failed to open %s\n",fname);
330 if (!S_ISREG(st.st_mode)) {
331 fprintf(stderr,"%s : not a regular file\n",fname);
335 if (update_only && st.st_mtime >= flist->files[i].modtime) {
337 fprintf(stderr,"%s is newer\n",fname);
341 if (always_checksum && S_ISREG(st.st_mode)) {
342 file_checksum(fname,sum,st.st_size);
345 if (st.st_size == flist->files[i].length &&
346 ((!ignore_times && st.st_mtime == flist->files[i].modtime) ||
347 (always_checksum && S_ISREG(st.st_mode) &&
348 memcmp(sum,flist->files[i].sum,csum_length) == 0))) {
349 set_perms(fname,&flist->files[i],&st,1);
359 fd = open(fname,O_RDONLY);
362 fprintf(stderr,"failed to open %s : %s\n",fname,strerror(errno));
366 if (st.st_size > 0) {
367 buf = map_file(fd,st.st_size);
373 fprintf(stderr,"mapped %s of size %d\n",fname,(int)st.st_size);
375 s = generate_sums(buf,st.st_size,block_size);
382 unmap_file(buf,st.st_size);
389 static void receive_data(int f_in,char *buf,int fd,char *fname)
391 int i,n,remainder,len,count;
395 count = read_int(f_in);
397 remainder = read_int(f_in);
399 for (i=read_int(f_in); i != 0; i=read_int(f_in)) {
402 fprintf(stderr,"data recv %d at %d\n",i,(int)offset);
404 if (read_write(f_in,fd,i) != i) {
405 fprintf(stderr,"write failed on %s : %s\n",fname,strerror(errno));
413 if (i == count-1 && remainder != 0)
417 fprintf(stderr,"chunk[%d] of size %d at %d offset=%d\n",
418 i,len,(int)offset2,(int)offset);
420 if (write(fd,map_ptr(buf,offset2,len),len) != len) {
421 fprintf(stderr,"write failed on %s : %s\n",fname,strerror(errno));
430 static void delete_one(struct file_struct *f)
432 if (!S_ISDIR(f->mode)) {
433 if (!dry_run && unlink(f->name) != 0) {
434 fprintf(stderr,"unlink %s : %s\n",f->name,strerror(errno));
435 } else if (verbose) {
436 fprintf(stderr,"deleting %s\n",f->name);
439 if (!dry_run && rmdir(f->name) != 0) {
440 if (errno != ENOTEMPTY)
441 fprintf(stderr,"rmdir %s : %s\n",f->name,strerror(errno));
442 } else if (verbose) {
443 fprintf(stderr,"deleting directory %s\n",f->name);
449 static void delete_files(struct file_list *flist)
451 struct file_list *local_file_list;
458 if (!(local_file_list = send_file_list(-1,recurse,1,&dot)))
461 for (i=local_file_list->count;i>=0;i--) {
462 if (!local_file_list->files[i].name) continue;
463 if (-1 == flist_find(flist,&local_file_list->files[i])) {
464 delete_one(&local_file_list->files[i]);
469 static char *cleanup_fname = NULL;
474 unlink(cleanup_fname);
479 int recv_files(int f_in,struct file_list *flist,char *local_name)
484 char fnametmp[MAXPATHLEN];
489 fprintf(stderr,"recv_files(%d) starting\n",flist->count);
492 if (recurse && delete_mode && !local_name && flist->count>0) {
501 fname = flist->files[i].name;
507 if (!am_server && verbose)
508 printf("%s\n",fname);
513 fprintf(stderr,"recv_files(%s)\n",fname);
516 fd1 = open(fname,O_RDONLY);
518 if (fd1 != -1 && fstat(fd1,&st) != 0) {
519 fprintf(stderr,"fstat %s : %s\n",fname,strerror(errno));
524 if (fd1 != -1 && !S_ISREG(st.st_mode)) {
525 fprintf(stderr,"%s : not a regular file\n",fname);
530 if (fd1 != -1 && st.st_size > 0) {
531 buf = map_file(fd1,st.st_size);
537 fprintf(stderr,"mapped %s of size %d\n",fname,(int)st.st_size);
540 sprintf(fnametmp,"%s.XXXXXX",fname);
541 if (NULL == mktemp(fnametmp)) {
542 fprintf(stderr,"mktemp %s failed\n",fnametmp);
545 fd2 = open(fnametmp,O_WRONLY|O_CREAT,flist->files[i].mode);
547 fprintf(stderr,"open %s : %s\n",fnametmp,strerror(errno));
551 cleanup_fname = fnametmp;
553 if (!am_server && verbose)
554 printf("%s\n",fname);
557 receive_data(f_in,buf,fd2,fname);
560 unmap_file(buf,st.st_size);
566 fprintf(stderr,"renaming %s to %s\n",fnametmp,fname);
569 char fnamebak[MAXPATHLEN];
570 sprintf(fnamebak,"%s%s",fname,backup_suffix);
571 if (rename(fname,fnamebak) != 0 && errno != ENOENT) {
572 fprintf(stderr,"rename %s %s : %s\n",fname,fnamebak,strerror(errno));
577 /* move tmp file over real file */
578 if (rename(fnametmp,fname) != 0) {
579 fprintf(stderr,"rename %s -> %s : %s\n",
580 fnametmp,fname,strerror(errno));
583 cleanup_fname = NULL;
585 set_perms(fname,&flist->files[i],NULL,0);
589 fprintf(stderr,"recv_files finished\n");
596 off_t send_files(struct file_list *flist,int f_out,int f_in)
599 struct sum_struct *s;
602 char fname[MAXPATHLEN];
607 fprintf(stderr,"send_files starting\n");
609 setup_nonblocking(f_in,f_out);
617 if (flist->files[i].dir) {
618 strcpy(fname,flist->files[i].dir);
621 strcat(fname,flist->files[i].name);
624 fprintf(stderr,"send_files(%d,%s)\n",i,fname);
627 if (!am_server && verbose)
628 printf("%s\n",fname);
633 s = receive_sums(f_in);
635 fprintf(stderr,"receive_sums failed\n");
639 fd = open(fname,O_RDONLY);
641 fprintf(stderr,"send_files failed to open %s: %s\n",
642 fname,strerror(errno));
646 /* map the local file */
647 if (fstat(fd,&st) != 0) {
648 fprintf(stderr,"fstat failed : %s\n",strerror(errno));
652 if (st.st_size > 0) {
653 buf = map_file(fd,st.st_size);
659 fprintf(stderr,"send_files mapped %s of size %d\n",
660 fname,(int)st.st_size);
664 write_int(f_out,s->count);
665 write_int(f_out,s->n);
666 write_int(f_out,s->remainder);
669 fprintf(stderr,"calling match_sums %s\n",fname);
671 if (!am_server && verbose)
672 printf("%s\n",fname);
674 match_sums(f_out,s,buf,st.st_size);
677 unmap_file(buf,st.st_size);
683 fprintf(stderr,"sender finished %s\n",fname);
698 void generate_files(int f,struct file_list *flist,char *local_name)
703 fprintf(stderr,"generator starting pid=%d count=%d\n",
704 (int)getpid(),flist->count);
706 for (i = 0; i < flist->count; i++) {
707 if (!flist->files[i].name) continue;
708 if (S_ISDIR(flist->files[i].mode)) {
709 if (dry_run) continue;
710 if (mkdir(flist->files[i].name,flist->files[i].mode) != 0 &&
712 fprintf(stderr,"mkdir %s : %s\n",
713 flist->files[i].name,strerror(errno));
717 recv_generator(local_name?local_name:flist->files[i].name,
723 fprintf(stderr,"generator wrote %d\n",write_total());