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_hard_links;
36 extern int preserve_perms;
37 extern int preserve_devices;
38 extern int preserve_uid;
39 extern int preserve_gid;
40 extern int preserve_times;
42 extern int ignore_times;
44 extern int delete_mode;
45 extern int cvs_exclude;
50 static void free_sums(struct sum_struct *s)
52 if (s->sums) free(s->sums);
59 send a sums struct down a fd
61 static void send_sums(struct sum_struct *s,int f_out)
65 /* tell the other guy how many we are going to be doing and how many
66 bytes there are in the last chunk */
67 write_int(f_out,s?s->count:0);
68 write_int(f_out,s?s->n:block_size);
69 write_int(f_out,s?s->remainder:0);
71 for (i=0;i<s->count;i++) {
72 write_int(f_out,s->sums[i].sum1);
73 write_buf(f_out,s->sums[i].sum2,csum_length);
80 generate a stream of signatures/checksums that describe a buffer
82 generate approximately one checksum every n bytes
84 static struct sum_struct *generate_sums(char *buf,off_t len,int n)
90 int remainder = (len%block_len);
93 count = (len+(block_len-1))/block_len;
95 s = (struct sum_struct *)malloc(sizeof(*s));
96 if (!s) out_of_memory("generate_sums");
99 s->remainder = remainder;
109 fprintf(FERROR,"count=%d rem=%d n=%d flength=%d\n",
110 s->count,s->remainder,s->n,(int)s->flength);
112 s->sums = (struct sum_buf *)malloc(sizeof(s->sums[0])*s->count);
113 if (!s->sums) out_of_memory("generate_sums");
115 for (i=0;i<count;i++) {
117 char *map = map_ptr(buf,offset,n1);
119 s->sums[i].sum1 = get_checksum1(map,n1);
120 get_checksum2(map,n1,s->sums[i].sum2);
122 s->sums[i].offset = offset;
127 fprintf(FERROR,"chunk[%d] offset=%d len=%d sum1=%08x\n",
128 i,(int)s->sums[i].offset,s->sums[i].len,s->sums[i].sum1);
139 receive the checksums for a buffer
141 static struct sum_struct *receive_sums(int f)
143 struct sum_struct *s;
148 s = (struct sum_struct *)malloc(sizeof(*s));
149 if (!s) out_of_memory("receive_sums");
151 s->count = read_int(f);
153 s->remainder = read_int(f);
157 fprintf(FERROR,"count=%d n=%d rem=%d\n",
158 s->count,s->n,s->remainder);
165 s->sums = (struct sum_buf *)malloc(sizeof(s->sums[0])*s->count);
166 if (!s->sums) out_of_memory("receive_sums");
168 for (i=0;i<s->count;i++) {
169 s->sums[i].sum1 = read_int(f);
170 read_buf(f,s->sums[i].sum2,csum_length);
172 s->sums[i].offset = offset;
175 if (i == s->count-1 && s->remainder != 0) {
176 s->sums[i].len = s->remainder;
178 s->sums[i].len = s->n;
180 offset += s->sums[i].len;
183 fprintf(FERROR,"chunk[%d] len=%d offset=%d sum1=%08x\n",
184 i,s->sums[i].len,(int)s->sums[i].offset,s->sums[i].sum1);
193 static void set_perms(char *fname,struct file_struct *file,struct stat *st,
202 if (stat(fname,&st2) != 0) {
203 fprintf(FERROR,"stat %s : %s\n",fname,strerror(errno));
209 if (preserve_times && !S_ISLNK(st->st_mode) &&
210 st->st_mtime != file->modtime) {
212 if (set_modtime(fname,file->modtime) != 0) {
213 fprintf(FERROR,"failed to set times on %s : %s\n",
214 fname,strerror(errno));
220 if (preserve_perms && !S_ISLNK(st->st_mode) &&
221 st->st_mode != file->mode) {
223 if (chmod(fname,file->mode) != 0) {
224 fprintf(FERROR,"failed to set permissions on %s : %s\n",
225 fname,strerror(errno));
231 if ((preserve_uid && st->st_uid != file->uid) ||
232 (preserve_gid && st->st_gid != file->gid)) {
235 preserve_uid?file->uid:-1,
236 preserve_gid?file->gid:-1) != 0) {
237 if (verbose>1 || preserve_uid)
238 fprintf(FERROR,"chown %s : %s\n",fname,strerror(errno));
243 if (verbose > 1 && report) {
245 fprintf(FINFO,"%s\n",fname);
247 fprintf(FINFO,"%s is uptodate\n",fname);
252 void recv_generator(char *fname,struct file_list *flist,int i,int f_out)
257 struct sum_struct *s;
258 char sum[SUM_LENGTH];
260 struct file_struct *file = &flist->files[i];
263 fprintf(FERROR,"recv_generator(%s)\n",fname);
265 statret = lstat(fname,&st);
268 if (preserve_links && S_ISLNK(file->mode)) {
269 char lnk[MAXPATHLEN];
272 l = readlink(fname,lnk,MAXPATHLEN-1);
275 if (strcmp(lnk,file->link) == 0) {
276 set_perms(fname,file,&st,1);
281 if (!dry_run) unlink(fname);
282 if (!dry_run && symlink(file->link,fname) != 0) {
283 fprintf(FERROR,"link %s -> %s : %s\n",
284 fname,file->link,strerror(errno));
286 set_perms(fname,file,NULL,0);
288 fprintf(FINFO,"%s -> %s\n",
296 if (preserve_devices && IS_DEVICE(file->mode)) {
298 st.st_mode != file->mode ||
299 st.st_rdev != file->rdev) {
300 if (!dry_run) unlink(fname);
302 fprintf(FERROR,"mknod(%s,0%o,0x%x)\n",
303 fname,(int)file->mode,(int)file->rdev);
305 mknod(fname,file->mode,file->rdev) != 0) {
306 fprintf(FERROR,"mknod %s : %s\n",fname,strerror(errno));
308 set_perms(fname,file,NULL,0);
310 fprintf(FINFO,"%s\n",fname);
313 set_perms(fname,file,&st,1);
319 if (preserve_hard_links && check_hard_link(file)) {
321 fprintf(FINFO,"%s is a hard link\n",file->name);
325 if (!S_ISREG(file->mode)) {
326 fprintf(FERROR,"skipping non-regular file %s\n",fname);
331 if (errno == ENOENT) {
333 if (!dry_run) send_sums(NULL,f_out);
336 fprintf(FERROR,"recv_generator failed to open %s\n",fname);
341 if (!S_ISREG(st.st_mode)) {
342 fprintf(FERROR,"%s : not a regular file\n",fname);
346 if (update_only && st.st_mtime >= file->modtime) {
348 fprintf(FERROR,"%s is newer\n",fname);
352 if (always_checksum && S_ISREG(st.st_mode)) {
353 file_checksum(fname,sum,st.st_size);
356 if (st.st_size == file->length &&
357 ((!ignore_times && st.st_mtime == file->modtime) ||
358 (always_checksum && S_ISREG(st.st_mode) &&
359 memcmp(sum,file->sum,csum_length) == 0))) {
360 set_perms(fname,file,&st,1);
370 fd = open(fname,O_RDONLY);
373 fprintf(FERROR,"failed to open %s : %s\n",fname,strerror(errno));
377 if (st.st_size > 0) {
378 buf = map_file(fd,st.st_size);
384 fprintf(FERROR,"mapped %s of size %d\n",fname,(int)st.st_size);
386 s = generate_sums(buf,st.st_size,block_size);
393 unmap_file(buf,st.st_size);
400 static void receive_data(int f_in,char *buf,int fd,char *fname)
402 int i,n,remainder,len,count;
406 count = read_int(f_in);
408 remainder = read_int(f_in);
410 for (i=read_int(f_in); i != 0; i=read_int(f_in)) {
413 fprintf(FERROR,"data recv %d at %d\n",i,(int)offset);
415 if (read_write(f_in,fd,i) != i) {
416 fprintf(FERROR,"write failed on %s : %s\n",fname,strerror(errno));
424 if (i == count-1 && remainder != 0)
428 fprintf(FERROR,"chunk[%d] of size %d at %d offset=%d\n",
429 i,len,(int)offset2,(int)offset);
431 if (write_sparse(fd,map_ptr(buf,offset2,len),len) != len) {
432 fprintf(FERROR,"write failed on %s : %s\n",fname,strerror(errno));
439 if (offset > 0 && sparse_end(fd) != 0) {
440 fprintf(FERROR,"write failed on %s : %s\n",fname,strerror(errno));
446 static void delete_one(struct file_struct *f)
448 if (!S_ISDIR(f->mode)) {
449 if (!dry_run && unlink(f->name) != 0) {
450 fprintf(FERROR,"unlink %s : %s\n",f->name,strerror(errno));
451 } else if (verbose) {
452 fprintf(FERROR,"deleting %s\n",f->name);
455 if (!dry_run && rmdir(f->name) != 0) {
456 if (errno != ENOTEMPTY)
457 fprintf(FERROR,"rmdir %s : %s\n",f->name,strerror(errno));
458 } else if (verbose) {
459 fprintf(FERROR,"deleting directory %s\n",f->name);
465 static void delete_files(struct file_list *flist)
467 struct file_list *local_file_list;
474 if (!(local_file_list = send_file_list(-1,recurse,1,&dot)))
477 for (i=local_file_list->count;i>=0;i--) {
478 if (!local_file_list->files[i].name) continue;
479 if (-1 == flist_find(flist,&local_file_list->files[i])) {
480 delete_one(&local_file_list->files[i]);
485 static char *cleanup_fname = NULL;
487 void exit_cleanup(int code)
490 unlink(cleanup_fname);
500 int recv_files(int f_in,struct file_list *flist,char *local_name)
505 char fnametmp[MAXPATHLEN];
508 struct file_struct *file;
511 fprintf(FERROR,"recv_files(%d) starting\n",flist->count);
514 if (recurse && delete_mode && !local_name && flist->count>0) {
523 file = &flist->files[i];
530 if (!am_server && verbose)
531 printf("%s\n",fname);
536 fprintf(FERROR,"recv_files(%s)\n",fname);
539 fd1 = open(fname,O_RDONLY);
541 if (fd1 != -1 && fstat(fd1,&st) != 0) {
542 fprintf(FERROR,"fstat %s : %s\n",fname,strerror(errno));
547 if (fd1 != -1 && !S_ISREG(st.st_mode)) {
548 fprintf(FERROR,"%s : not a regular file\n",fname);
553 if (fd1 != -1 && st.st_size > 0) {
554 buf = map_file(fd1,st.st_size);
560 fprintf(FERROR,"mapped %s of size %d\n",fname,(int)st.st_size);
563 sprintf(fnametmp,"%s.XXXXXX",fname);
564 if (NULL == mktemp(fnametmp)) {
565 fprintf(FERROR,"mktemp %s failed\n",fnametmp);
568 fd2 = open(fnametmp,O_WRONLY|O_CREAT,file->mode);
570 fprintf(FERROR,"open %s : %s\n",fnametmp,strerror(errno));
574 cleanup_fname = fnametmp;
576 if (!am_server && verbose)
577 printf("%s\n",fname);
580 receive_data(f_in,buf,fd2,fname);
583 unmap_file(buf,st.st_size);
589 fprintf(FERROR,"renaming %s to %s\n",fnametmp,fname);
592 char fnamebak[MAXPATHLEN];
593 sprintf(fnamebak,"%s%s",fname,backup_suffix);
594 if (rename(fname,fnamebak) != 0 && errno != ENOENT) {
595 fprintf(FERROR,"rename %s %s : %s\n",fname,fnamebak,strerror(errno));
600 /* move tmp file over real file */
601 if (rename(fnametmp,fname) != 0) {
602 fprintf(FERROR,"rename %s -> %s : %s\n",
603 fnametmp,fname,strerror(errno));
606 cleanup_fname = NULL;
608 set_perms(fname,file,NULL,0);
612 fprintf(FERROR,"recv_files finished\n");
619 off_t send_files(struct file_list *flist,int f_out,int f_in)
622 struct sum_struct *s;
625 char fname[MAXPATHLEN];
628 struct file_struct *file;
631 fprintf(FERROR,"send_files starting\n");
633 setup_nonblocking(f_in,f_out);
640 file = &flist->files[i];
644 strcpy(fname,file->dir);
647 strcat(fname,file->name);
650 fprintf(FERROR,"send_files(%d,%s)\n",i,fname);
653 if (!am_server && verbose)
654 printf("%s\n",fname);
659 s = receive_sums(f_in);
661 fprintf(FERROR,"receive_sums failed\n");
665 fd = open(fname,O_RDONLY);
667 fprintf(FERROR,"send_files failed to open %s: %s\n",
668 fname,strerror(errno));
672 /* map the local file */
673 if (fstat(fd,&st) != 0) {
674 fprintf(FERROR,"fstat failed : %s\n",strerror(errno));
678 if (st.st_size > 0) {
679 buf = map_file(fd,st.st_size);
685 fprintf(FERROR,"send_files mapped %s of size %d\n",
686 fname,(int)st.st_size);
690 write_int(f_out,s->count);
691 write_int(f_out,s->n);
692 write_int(f_out,s->remainder);
695 fprintf(FERROR,"calling match_sums %s\n",fname);
697 if (!am_server && verbose)
698 printf("%s\n",fname);
700 match_sums(f_out,s,buf,st.st_size);
703 unmap_file(buf,st.st_size);
709 fprintf(FERROR,"sender finished %s\n",fname);
715 fprintf(FERROR,"send files finished\n");
727 void generate_files(int f,struct file_list *flist,char *local_name)
732 fprintf(FERROR,"generator starting pid=%d count=%d\n",
733 (int)getpid(),flist->count);
735 for (i = 0; i < flist->count; i++) {
736 struct file_struct *file = &flist->files[i];
737 if (!file->name) continue;
738 if (S_ISDIR(file->mode)) {
739 if (dry_run) continue;
740 if (mkdir(file->name,file->mode) != 0 &&
742 fprintf(FERROR,"mkdir %s : %s\n",
743 file->name,strerror(errno));
747 recv_generator(local_name?local_name:file->name,
753 fprintf(FERROR,"generator wrote %d\n",write_total());