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 && !S_ISLNK(st->st_mode) &&
209 st->st_mtime != file->modtime) {
211 if (set_modtime(fname,file->modtime) != 0) {
212 fprintf(stderr,"failed to set times on %s : %s\n",
213 fname,strerror(errno));
219 if (preserve_perms && !S_ISLNK(st->st_mode) &&
220 st->st_mode != file->mode) {
222 if (chmod(fname,file->mode) != 0) {
223 fprintf(stderr,"failed to set permissions on %s : %s\n",
224 fname,strerror(errno));
230 if ((preserve_uid && st->st_uid != file->uid) ||
231 (preserve_gid && st->st_gid != file->gid)) {
234 preserve_uid?file->uid:-1,
235 preserve_gid?file->gid:-1) != 0) {
236 if (verbose>1 || preserve_uid)
237 fprintf(stderr,"chown %s : %s\n",fname,strerror(errno));
242 if (verbose > 1 && report) {
244 fprintf(am_server?stderr:stdout,"%s\n",fname);
246 fprintf(am_server?stderr:stdout,"%s is uptodate\n",fname);
251 void recv_generator(char *fname,struct file_list *flist,int i,int f_out)
256 struct sum_struct *s;
257 char sum[SUM_LENGTH];
261 fprintf(stderr,"recv_generator(%s)\n",fname);
263 statret = lstat(fname,&st);
266 if (preserve_links && S_ISLNK(flist->files[i].mode)) {
267 char lnk[MAXPATHLEN];
270 l = readlink(fname,lnk,MAXPATHLEN-1);
273 if (strcmp(lnk,flist->files[i].link) == 0) {
274 set_perms(fname,&flist->files[i],&st,1);
279 if (!dry_run) unlink(fname);
280 if (!dry_run && symlink(flist->files[i].link,fname) != 0) {
281 fprintf(stderr,"link %s -> %s : %s\n",
282 fname,flist->files[i].link,strerror(errno));
284 set_perms(fname,&flist->files[i],NULL,0);
286 fprintf(am_server?stderr:stdout,"%s -> %s\n",
287 fname,flist->files[i].link);
294 if (preserve_devices && IS_DEVICE(flist->files[i].mode)) {
296 st.st_mode != flist->files[i].mode ||
297 st.st_rdev != flist->files[i].dev) {
298 if (!dry_run) unlink(fname);
300 fprintf(stderr,"mknod(%s,0%o,0x%x)\n",
301 fname,(int)flist->files[i].mode,(int)flist->files[i].dev);
303 mknod(fname,flist->files[i].mode,flist->files[i].dev) != 0) {
304 fprintf(stderr,"mknod %s : %s\n",fname,strerror(errno));
306 set_perms(fname,&flist->files[i],NULL,0);
308 fprintf(am_server?stderr:stdout,"%s\n",fname);
311 set_perms(fname,&flist->files[i],&st,1);
317 if (!S_ISREG(flist->files[i].mode)) {
318 fprintf(stderr,"skipping non-regular file %s\n",fname);
323 if (errno == ENOENT) {
325 if (!dry_run) send_sums(NULL,f_out);
328 fprintf(stderr,"recv_generator failed to open %s\n",fname);
333 if (!S_ISREG(st.st_mode)) {
334 fprintf(stderr,"%s : not a regular file\n",fname);
338 if (update_only && st.st_mtime >= flist->files[i].modtime) {
340 fprintf(stderr,"%s is newer\n",fname);
344 if (always_checksum && S_ISREG(st.st_mode)) {
345 file_checksum(fname,sum,st.st_size);
348 if (st.st_size == flist->files[i].length &&
349 ((!ignore_times && st.st_mtime == flist->files[i].modtime) ||
350 (always_checksum && S_ISREG(st.st_mode) &&
351 memcmp(sum,flist->files[i].sum,csum_length) == 0))) {
352 set_perms(fname,&flist->files[i],&st,1);
362 fd = open(fname,O_RDONLY);
365 fprintf(stderr,"failed to open %s : %s\n",fname,strerror(errno));
369 if (st.st_size > 0) {
370 buf = map_file(fd,st.st_size);
376 fprintf(stderr,"mapped %s of size %d\n",fname,(int)st.st_size);
378 s = generate_sums(buf,st.st_size,block_size);
385 unmap_file(buf,st.st_size);
392 static void receive_data(int f_in,char *buf,int fd,char *fname)
394 int i,n,remainder,len,count;
398 count = read_int(f_in);
400 remainder = read_int(f_in);
402 for (i=read_int(f_in); i != 0; i=read_int(f_in)) {
405 fprintf(stderr,"data recv %d at %d\n",i,(int)offset);
407 if (read_write(f_in,fd,i) != i) {
408 fprintf(stderr,"write failed on %s : %s\n",fname,strerror(errno));
416 if (i == count-1 && remainder != 0)
420 fprintf(stderr,"chunk[%d] of size %d at %d offset=%d\n",
421 i,len,(int)offset2,(int)offset);
423 if (write_sparse(fd,map_ptr(buf,offset2,len),len) != len) {
424 fprintf(stderr,"write failed on %s : %s\n",fname,strerror(errno));
431 if (offset > 0 && sparse_end(fd) != 0) {
432 fprintf(stderr,"write failed on %s : %s\n",fname,strerror(errno));
438 static void delete_one(struct file_struct *f)
440 if (!S_ISDIR(f->mode)) {
441 if (!dry_run && unlink(f->name) != 0) {
442 fprintf(stderr,"unlink %s : %s\n",f->name,strerror(errno));
443 } else if (verbose) {
444 fprintf(stderr,"deleting %s\n",f->name);
447 if (!dry_run && rmdir(f->name) != 0) {
448 if (errno != ENOTEMPTY)
449 fprintf(stderr,"rmdir %s : %s\n",f->name,strerror(errno));
450 } else if (verbose) {
451 fprintf(stderr,"deleting directory %s\n",f->name);
457 static void delete_files(struct file_list *flist)
459 struct file_list *local_file_list;
466 if (!(local_file_list = send_file_list(-1,recurse,1,&dot)))
469 for (i=local_file_list->count;i>=0;i--) {
470 if (!local_file_list->files[i].name) continue;
471 if (-1 == flist_find(flist,&local_file_list->files[i])) {
472 delete_one(&local_file_list->files[i]);
477 static char *cleanup_fname = NULL;
479 void exit_cleanup(int code)
482 unlink(cleanup_fname);
492 int recv_files(int f_in,struct file_list *flist,char *local_name)
497 char fnametmp[MAXPATHLEN];
502 fprintf(stderr,"recv_files(%d) starting\n",flist->count);
505 if (recurse && delete_mode && !local_name && flist->count>0) {
514 fname = flist->files[i].name;
520 if (!am_server && verbose)
521 printf("%s\n",fname);
526 fprintf(stderr,"recv_files(%s)\n",fname);
529 fd1 = open(fname,O_RDONLY);
531 if (fd1 != -1 && fstat(fd1,&st) != 0) {
532 fprintf(stderr,"fstat %s : %s\n",fname,strerror(errno));
537 if (fd1 != -1 && !S_ISREG(st.st_mode)) {
538 fprintf(stderr,"%s : not a regular file\n",fname);
543 if (fd1 != -1 && st.st_size > 0) {
544 buf = map_file(fd1,st.st_size);
550 fprintf(stderr,"mapped %s of size %d\n",fname,(int)st.st_size);
553 sprintf(fnametmp,"%s.XXXXXX",fname);
554 if (NULL == mktemp(fnametmp)) {
555 fprintf(stderr,"mktemp %s failed\n",fnametmp);
558 fd2 = open(fnametmp,O_WRONLY|O_CREAT,flist->files[i].mode);
560 fprintf(stderr,"open %s : %s\n",fnametmp,strerror(errno));
564 cleanup_fname = fnametmp;
566 if (!am_server && verbose)
567 printf("%s\n",fname);
570 receive_data(f_in,buf,fd2,fname);
573 unmap_file(buf,st.st_size);
579 fprintf(stderr,"renaming %s to %s\n",fnametmp,fname);
582 char fnamebak[MAXPATHLEN];
583 sprintf(fnamebak,"%s%s",fname,backup_suffix);
584 if (rename(fname,fnamebak) != 0 && errno != ENOENT) {
585 fprintf(stderr,"rename %s %s : %s\n",fname,fnamebak,strerror(errno));
590 /* move tmp file over real file */
591 if (rename(fnametmp,fname) != 0) {
592 fprintf(stderr,"rename %s -> %s : %s\n",
593 fnametmp,fname,strerror(errno));
596 cleanup_fname = NULL;
598 set_perms(fname,&flist->files[i],NULL,0);
602 fprintf(stderr,"recv_files finished\n");
609 off_t send_files(struct file_list *flist,int f_out,int f_in)
612 struct sum_struct *s;
615 char fname[MAXPATHLEN];
620 fprintf(stderr,"send_files starting\n");
622 setup_nonblocking(f_in,f_out);
630 if (flist->files[i].dir) {
631 strcpy(fname,flist->files[i].dir);
634 strcat(fname,flist->files[i].name);
637 fprintf(stderr,"send_files(%d,%s)\n",i,fname);
640 if (!am_server && verbose)
641 printf("%s\n",fname);
646 s = receive_sums(f_in);
648 fprintf(stderr,"receive_sums failed\n");
652 fd = open(fname,O_RDONLY);
654 fprintf(stderr,"send_files failed to open %s: %s\n",
655 fname,strerror(errno));
659 /* map the local file */
660 if (fstat(fd,&st) != 0) {
661 fprintf(stderr,"fstat failed : %s\n",strerror(errno));
665 if (st.st_size > 0) {
666 buf = map_file(fd,st.st_size);
672 fprintf(stderr,"send_files mapped %s of size %d\n",
673 fname,(int)st.st_size);
677 write_int(f_out,s->count);
678 write_int(f_out,s->n);
679 write_int(f_out,s->remainder);
682 fprintf(stderr,"calling match_sums %s\n",fname);
684 if (!am_server && verbose)
685 printf("%s\n",fname);
687 match_sums(f_out,s,buf,st.st_size);
690 unmap_file(buf,st.st_size);
696 fprintf(stderr,"sender finished %s\n",fname);
711 void generate_files(int f,struct file_list *flist,char *local_name)
716 fprintf(stderr,"generator starting pid=%d count=%d\n",
717 (int)getpid(),flist->count);
719 for (i = 0; i < flist->count; i++) {
720 if (!flist->files[i].name) continue;
721 if (S_ISDIR(flist->files[i].mode)) {
722 if (dry_run) continue;
723 if (mkdir(flist->files[i].name,flist->files[i].mode) != 0 &&
725 fprintf(stderr,"mkdir %s : %s\n",
726 flist->files[i].name,strerror(errno));
730 recv_generator(local_name?local_name:flist->files[i].name,
736 fprintf(stderr,"generator wrote %d\n",write_total());