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.
24 extern int always_checksum;
25 extern time_t starttime;
27 extern char *backup_suffix;
29 extern int block_size;
30 extern int update_only;
31 extern int make_backups;
32 extern int preserve_links;
33 extern int preserve_perms;
34 extern int preserve_devices;
35 extern int preserve_uid;
36 extern int preserve_gid;
37 extern int preserve_times;
39 extern int ignore_times;
41 extern int delete_mode;
42 extern int cvs_exclude;
47 static void free_sums(struct sum_struct *s)
49 if (s->sums) free(s->sums);
56 send a sums struct down a fd
58 static void send_sums(struct sum_struct *s,int f_out)
62 /* tell the other guy how many we are going to be doing and how many
63 bytes there are in the last chunk */
64 write_int(f_out,s?s->count:0);
65 write_int(f_out,s?s->n:block_size);
66 write_int(f_out,s?s->remainder:0);
68 for (i=0;i<s->count;i++) {
69 write_int(f_out,s->sums[i].sum1);
70 write_buf(f_out,s->sums[i].sum2,SUM_LENGTH);
77 generate a stream of signatures/checksums that describe a buffer
79 generate approximately one checksum every n bytes
81 static struct sum_struct *generate_sums(char *buf,off_t len,int n)
87 int remainder = (len%block_len);
90 count = (len+(block_len-1))/block_len;
92 s = (struct sum_struct *)malloc(sizeof(*s));
93 if (!s) out_of_memory("generate_sums");
96 s->remainder = remainder;
106 fprintf(stderr,"count=%d rem=%d n=%d flength=%d\n",
107 s->count,s->remainder,s->n,(int)s->flength);
109 s->sums = (struct sum_buf *)malloc(sizeof(s->sums[0])*s->count);
110 if (!s->sums) out_of_memory("generate_sums");
112 for (i=0;i<count;i++) {
115 s->sums[i].sum1 = get_checksum1(buf,n1);
116 get_checksum2(buf,n1,s->sums[i].sum2);
118 s->sums[i].offset = offset;
123 fprintf(stderr,"chunk[%d] offset=%d len=%d sum1=%08x\n",
124 i,(int)s->sums[i].offset,s->sums[i].len,s->sums[i].sum1);
136 receive the checksums for a buffer
138 static struct sum_struct *receive_sums(int f)
140 struct sum_struct *s;
145 s = (struct sum_struct *)malloc(sizeof(*s));
146 if (!s) out_of_memory("receive_sums");
148 s->count = read_int(f);
150 s->remainder = read_int(f);
154 fprintf(stderr,"count=%d n=%d rem=%d\n",
155 s->count,s->n,s->remainder);
162 s->sums = (struct sum_buf *)malloc(sizeof(s->sums[0])*s->count);
163 if (!s->sums) out_of_memory("receive_sums");
165 for (i=0;i<s->count;i++) {
166 s->sums[i].sum1 = read_int(f);
167 read_buf(f,s->sums[i].sum2,SUM_LENGTH);
169 s->sums[i].offset = offset;
172 if (i == s->count-1 && s->remainder != 0) {
173 s->sums[i].len = s->remainder;
175 s->sums[i].len = s->n;
177 offset += s->sums[i].len;
180 fprintf(stderr,"chunk[%d] len=%d offset=%d sum1=%08x\n",
181 i,s->sums[i].len,(int)s->sums[i].offset,s->sums[i].sum1);
190 static void set_perms(char *fname,struct file_struct *file,struct stat *st,
199 if (stat(fname,&st2) != 0) {
200 fprintf(stderr,"stat %s : %s\n",fname,strerror(errno));
206 if (preserve_times && st->st_mtime != file->modtime) {
208 if (set_modtime(fname,file->modtime) != 0) {
209 fprintf(stderr,"failed to set times on %s : %s\n",
210 fname,strerror(errno));
216 if (preserve_perms && st->st_mode != file->mode) {
218 if (chmod(fname,file->mode) != 0) {
219 fprintf(stderr,"failed to set permissions on %s : %s\n",
220 fname,strerror(errno));
226 if ((preserve_uid && st->st_uid != file->uid) ||
227 (preserve_gid && st->st_gid != file->gid)) {
230 preserve_uid?file->uid:-1,
231 preserve_gid?file->gid:-1) != 0) {
232 if (verbose>1 || preserve_uid)
233 fprintf(stderr,"chown %s : %s\n",fname,strerror(errno));
238 if (verbose > 1 && report) {
240 fprintf(am_server?stderr:stdout,"%s\n",fname);
242 fprintf(am_server?stderr:stdout,"%s is uptodate\n",fname);
247 void recv_generator(char *fname,struct file_list *flist,int i,int f_out)
252 struct sum_struct *s;
253 char sum[SUM_LENGTH];
257 fprintf(stderr,"recv_generator(%s)\n",fname);
259 statret = lstat(fname,&st);
262 if (preserve_links && S_ISLNK(flist->files[i].mode)) {
263 char lnk[MAXPATHLEN];
266 l = readlink(fname,lnk,MAXPATHLEN-1);
269 if (strcmp(lnk,flist->files[i].link) == 0) {
271 fprintf(am_server?stderr:stdout,"%s is uptodate\n",fname);
276 if (!dry_run) unlink(fname);
277 if (!dry_run && symlink(flist->files[i].link,fname) != 0) {
278 fprintf(stderr,"link %s -> %s : %s\n",
279 fname,flist->files[i].link,strerror(errno));
282 fprintf(am_server?stderr:stdout,"%s -> %s\n",fname,flist->files[i].link);
289 if (preserve_devices &&
290 (S_ISCHR(flist->files[i].mode) || S_ISBLK(flist->files[i].mode))) {
292 st.st_mode != flist->files[i].mode ||
293 st.st_rdev != flist->files[i].dev) {
294 if (!dry_run) unlink(fname);
296 fprintf(stderr,"mknod(%s,0%o,0x%x)\n",
297 fname,(int)flist->files[i].mode,(int)flist->files[i].dev);
299 mknod(fname,flist->files[i].mode,flist->files[i].dev) != 0) {
300 fprintf(stderr,"mknod %s : %s\n",fname,strerror(errno));
302 set_perms(fname,&flist->files[i],NULL,0);
304 fprintf(am_server?stderr:stdout,"%s\n",fname);
307 set_perms(fname,&flist->files[i],&st,1);
313 if (!S_ISREG(flist->files[i].mode)) {
314 fprintf(stderr,"skipping non-regular file %s\n",fname);
319 if (errno == ENOENT) {
321 if (!dry_run) send_sums(NULL,f_out);
324 fprintf(stderr,"recv_generator failed to open %s\n",fname);
329 if (!S_ISREG(st.st_mode)) {
330 fprintf(stderr,"%s : not a regular file\n",fname);
334 if (update_only && st.st_mtime >= flist->files[i].modtime) {
336 fprintf(stderr,"%s is newer\n",fname);
340 if (always_checksum && S_ISREG(st.st_mode)) {
341 file_checksum(fname,sum,st.st_size);
344 if (st.st_size == flist->files[i].length &&
345 ((!ignore_times && st.st_mtime == flist->files[i].modtime) ||
346 (always_checksum && S_ISREG(st.st_mode) &&
347 memcmp(sum,flist->files[i].sum,SUM_LENGTH) == 0))) {
348 set_perms(fname,&flist->files[i],&st,1);
358 fd = open(fname,O_RDONLY);
361 fprintf(stderr,"failed to open %s : %s\n",fname,strerror(errno));
365 if (st.st_size > 0) {
366 buf = map_file(fd,st.st_size);
368 fprintf(stderr,"mmap : %s\n",strerror(errno));
377 fprintf(stderr,"mapped %s of size %d\n",fname,(int)st.st_size);
379 s = generate_sums(buf,st.st_size,block_size);
386 unmap_file(buf,st.st_size);
393 static void receive_data(int f_in,char *buf,int fd,char *fname)
395 int i,n,remainder,len,count;
399 count = read_int(f_in);
401 remainder = read_int(f_in);
403 for (i=read_int(f_in); i != 0; i=read_int(f_in)) {
406 fprintf(stderr,"data recv %d at %d\n",i,(int)offset);
408 if (read_write(f_in,fd,i) != i) {
409 fprintf(stderr,"write failed on %s : %s\n",fname,strerror(errno));
417 if (i == count-1 && remainder != 0)
421 fprintf(stderr,"chunk[%d] of size %d at %d offset=%d\n",
422 i,len,(int)offset2,(int)offset);
424 if (write(fd,buf+offset2,len) != len) {
425 fprintf(stderr,"write failed on %s : %s\n",fname,strerror(errno));
434 static void delete_one(struct file_struct *f)
436 if (!S_ISDIR(f->mode)) {
437 if (!dry_run && unlink(f->name) != 0) {
438 fprintf(stderr,"unlink %s : %s\n",f->name,strerror(errno));
439 } else if (verbose) {
440 fprintf(stderr,"deleting %s\n",f->name);
443 if (!dry_run && rmdir(f->name) != 0) {
444 if (errno != ENOTEMPTY)
445 fprintf(stderr,"rmdir %s : %s\n",f->name,strerror(errno));
446 } else if (verbose) {
447 fprintf(stderr,"deleting directory %s\n",f->name);
453 static void delete_files(struct file_list *flist)
455 struct file_list *local_file_list;
459 if (!(local_file_list = send_file_list(-1,recurse,1,&dot)))
462 for (i=local_file_list->count;i>=0;i--) {
463 if (!local_file_list->files[i].name) continue;
464 if (-1 == flist_find(flist,&local_file_list->files[i])) {
465 delete_one(&local_file_list->files[i]);
470 static char *cleanup_fname = NULL;
475 unlink(cleanup_fname);
480 int recv_files(int f_in,struct file_list *flist,char *local_name)
485 char fnametmp[MAXPATHLEN];
490 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 if ((fd1 = open(fname,O_RDONLY)) == -1 &&
517 (fd1 = open(fname,O_RDONLY|O_CREAT,flist->files[i].mode)) == -1) {
518 fprintf(stderr,"recv_files failed to open %s\n",fname);
522 if (fstat(fd1,&st) != 0) {
523 fprintf(stderr,"fstat %s : %s\n",fname,strerror(errno));
528 if (!S_ISREG(st.st_mode)) {
529 fprintf(stderr,"%s : not a regular file\n",fname);
534 if (st.st_size > 0) {
535 buf = map_file(fd1,st.st_size);
537 fprintf(stderr,"map_file failed\n");
545 fprintf(stderr,"mapped %s of size %d\n",fname,(int)st.st_size);
548 sprintf(fnametmp,"%s.XXXXXX",fname);
549 if (NULL == mktemp(fnametmp)) {
550 fprintf(stderr,"mktemp %s failed\n",fnametmp);
553 fd2 = open(fnametmp,O_WRONLY|O_CREAT,st.st_mode);
555 fprintf(stderr,"open %s : %s\n",fnametmp,strerror(errno));
559 cleanup_fname = fnametmp;
561 if (!am_server && verbose)
562 printf("%s\n",fname);
565 receive_data(f_in,buf,fd2,fname);
571 fprintf(stderr,"renaming %s to %s\n",fnametmp,fname);
574 char fnamebak[MAXPATHLEN];
575 sprintf(fnamebak,"%s%s",fname,backup_suffix);
576 if (rename(fname,fnamebak) != 0) {
577 fprintf(stderr,"rename %s %s : %s\n",fname,fnamebak,strerror(errno));
582 /* move tmp file over real file */
583 if (rename(fnametmp,fname) != 0) {
584 fprintf(stderr,"rename %s -> %s : %s\n",
585 fnametmp,fname,strerror(errno));
588 cleanup_fname = NULL;
590 unmap_file(buf,st.st_size);
592 set_perms(fname,&flist->files[i],NULL,0);
596 fprintf(stderr,"recv_files finished\n");
603 off_t send_files(struct file_list *flist,int f_out,int f_in)
606 struct sum_struct *s;
609 char fname[MAXPATHLEN];
614 fprintf(stderr,"send_files starting\n");
622 if (flist->files[i].dir) {
623 strcpy(fname,flist->files[i].dir);
626 strcat(fname,flist->files[i].name);
629 fprintf(stderr,"send_files(%d,%s)\n",i,fname);
632 if (!am_server && verbose)
633 printf("%s\n",fname);
638 s = receive_sums(f_in);
640 fprintf(stderr,"receive_sums failed\n");
644 fd = open(fname,O_RDONLY);
646 fprintf(stderr,"send_files failed to open %s: %s\n",
647 fname,strerror(errno));
651 /* map the local file */
652 if (fstat(fd,&st) != 0) {
653 fprintf(stderr,"fstat failed : %s\n",strerror(errno));
657 if (st.st_size > 0) {
658 buf = map_file(fd,st.st_size);
660 fprintf(stderr,"map_file failed : %s\n",strerror(errno));
668 fprintf(stderr,"send_files mapped %s of size %d\n",
669 fname,(int)st.st_size);
673 write_int(f_out,s->count);
674 write_int(f_out,s->n);
675 write_int(f_out,s->remainder);
678 fprintf(stderr,"calling match_sums %s\n",fname);
680 if (!am_server && verbose)
681 printf("%s\n",fname);
683 match_sums(f_out,s,buf,st.st_size);
686 unmap_file(buf,st.st_size);
692 fprintf(stderr,"sender finished %s\n",fname);
707 void generate_files(int f,struct file_list *flist,char *local_name)
712 fprintf(stderr,"generator starting pid=%d count=%d\n",
713 (int)getpid(),flist->count);
715 for (i = 0; i < flist->count; i++) {
716 if (!flist->files[i].name) continue;
717 if (S_ISDIR(flist->files[i].mode)) {
718 if (dry_run) continue;
719 if (mkdir(flist->files[i].name,flist->files[i].mode) != 0 &&
721 fprintf(stderr,"mkdir %s : %s\n",
722 flist->files[i].name,strerror(errno));
726 recv_generator(local_name?local_name:flist->files[i].name,
732 fprintf(stderr,"generator wrote %d\n",write_total());