minor cleanups (unused variables etc)
[rsync/rsync.git] / rsync.c
CommitLineData
c627d613
AT
1/*
2 Copyright (C) Andrew Tridgell 1996
3 Copyright (C) Paul Mackerras 1996
4
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.
9
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.
14
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.
18*/
19
20#include "rsync.h"
21
43a481dc
AT
22extern int csum_length;
23
c627d613
AT
24extern int verbose;
25extern int am_server;
26extern int always_checksum;
27extern time_t starttime;
28
c6e7fcb4
AT
29extern int remote_version;
30
c627d613
AT
31extern char *backup_suffix;
32
33extern int block_size;
34extern int update_only;
35extern int make_backups;
36extern int preserve_links;
dc5ddbcc 37extern int preserve_hard_links;
c627d613
AT
38extern int preserve_perms;
39extern int preserve_devices;
40extern int preserve_uid;
41extern int preserve_gid;
42extern int preserve_times;
43extern int dry_run;
44extern int ignore_times;
45extern int recurse;
46extern int delete_mode;
47extern int cvs_exclude;
7b8356d0 48extern int am_root;
6574b4f7 49extern int relative_paths;
c627d613
AT
50
51/*
52 free a sums struct
53 */
54static void free_sums(struct sum_struct *s)
55{
56 if (s->sums) free(s->sums);
57 free(s);
58}
59
60
61
62/*
63 send a sums struct down a fd
64 */
65static void send_sums(struct sum_struct *s,int f_out)
66{
67 int i;
68
69 /* tell the other guy how many we are going to be doing and how many
70 bytes there are in the last chunk */
71 write_int(f_out,s?s->count:0);
72 write_int(f_out,s?s->n:block_size);
73 write_int(f_out,s?s->remainder:0);
74 if (s)
75 for (i=0;i<s->count;i++) {
76 write_int(f_out,s->sums[i].sum1);
43a481dc 77 write_buf(f_out,s->sums[i].sum2,csum_length);
c627d613
AT
78 }
79 write_flush(f_out);
80}
81
82
83/*
84 generate a stream of signatures/checksums that describe a buffer
85
86 generate approximately one checksum every n bytes
87 */
c6e7fcb4 88static struct sum_struct *generate_sums(struct map_struct *buf,off_t len,int n)
c627d613
AT
89{
90 int i;
91 struct sum_struct *s;
92 int count;
93 int block_len = n;
94 int remainder = (len%block_len);
95 off_t offset = 0;
96
97 count = (len+(block_len-1))/block_len;
98
99 s = (struct sum_struct *)malloc(sizeof(*s));
100 if (!s) out_of_memory("generate_sums");
101
102 s->count = count;
103 s->remainder = remainder;
104 s->n = n;
105 s->flength = len;
106
107 if (count==0) {
108 s->sums = NULL;
109 return s;
110 }
111
112 if (verbose > 3)
dc5ddbcc 113 fprintf(FERROR,"count=%d rem=%d n=%d flength=%d\n",
c627d613
AT
114 s->count,s->remainder,s->n,(int)s->flength);
115
116 s->sums = (struct sum_buf *)malloc(sizeof(s->sums[0])*s->count);
117 if (!s->sums) out_of_memory("generate_sums");
118
119 for (i=0;i<count;i++) {
120 int n1 = MIN(len,n);
d9bea2dd 121 char *map = map_ptr(buf,offset,n1);
c627d613 122
d9bea2dd
AT
123 s->sums[i].sum1 = get_checksum1(map,n1);
124 get_checksum2(map,n1,s->sums[i].sum2);
c627d613
AT
125
126 s->sums[i].offset = offset;
127 s->sums[i].len = n1;
128 s->sums[i].i = i;
129
130 if (verbose > 3)
dc5ddbcc 131 fprintf(FERROR,"chunk[%d] offset=%d len=%d sum1=%08x\n",
c627d613
AT
132 i,(int)s->sums[i].offset,s->sums[i].len,s->sums[i].sum1);
133
134 len -= n1;
c627d613
AT
135 offset += n1;
136 }
137
138 return s;
139}
140
141
142/*
143 receive the checksums for a buffer
144 */
145static struct sum_struct *receive_sums(int f)
146{
147 struct sum_struct *s;
148 int i;
149 off_t offset = 0;
c627d613
AT
150
151 s = (struct sum_struct *)malloc(sizeof(*s));
152 if (!s) out_of_memory("receive_sums");
153
154 s->count = read_int(f);
155 s->n = read_int(f);
156 s->remainder = read_int(f);
157 s->sums = NULL;
158
159 if (verbose > 3)
dc5ddbcc 160 fprintf(FERROR,"count=%d n=%d rem=%d\n",
c627d613
AT
161 s->count,s->n,s->remainder);
162
c627d613
AT
163 if (s->count == 0)
164 return(s);
165
166 s->sums = (struct sum_buf *)malloc(sizeof(s->sums[0])*s->count);
167 if (!s->sums) out_of_memory("receive_sums");
168
169 for (i=0;i<s->count;i++) {
170 s->sums[i].sum1 = read_int(f);
43a481dc 171 read_buf(f,s->sums[i].sum2,csum_length);
c627d613
AT
172
173 s->sums[i].offset = offset;
174 s->sums[i].i = i;
175
176 if (i == s->count-1 && s->remainder != 0) {
177 s->sums[i].len = s->remainder;
178 } else {
179 s->sums[i].len = s->n;
180 }
181 offset += s->sums[i].len;
182
183 if (verbose > 3)
dc5ddbcc 184 fprintf(FERROR,"chunk[%d] len=%d offset=%d sum1=%08x\n",
c627d613
AT
185 i,s->sums[i].len,(int)s->sums[i].offset,s->sums[i].sum1);
186 }
187
188 s->flength = offset;
189
190 return s;
191}
192
193
7b8356d0
AT
194static int set_perms(char *fname,struct file_struct *file,struct stat *st,
195 int report)
c627d613
AT
196{
197 int updated = 0;
198 struct stat st2;
199
7b8356d0 200 if (dry_run) return 0;
c627d613
AT
201
202 if (!st) {
7b8356d0 203 if (lstat(fname,&st2) != 0) {
dc5ddbcc 204 fprintf(FERROR,"stat %s : %s\n",fname,strerror(errno));
7b8356d0 205 return 0;
c627d613
AT
206 }
207 st = &st2;
208 }
209
7bec6a5c
AT
210 if (preserve_times && !S_ISLNK(st->st_mode) &&
211 st->st_mtime != file->modtime) {
c627d613
AT
212 updated = 1;
213 if (set_modtime(fname,file->modtime) != 0) {
dc5ddbcc 214 fprintf(FERROR,"failed to set times on %s : %s\n",
c627d613 215 fname,strerror(errno));
7b8356d0 216 return 0;
c627d613
AT
217 }
218 }
219
220#ifdef HAVE_CHMOD
7bec6a5c
AT
221 if (preserve_perms && !S_ISLNK(st->st_mode) &&
222 st->st_mode != file->mode) {
c627d613
AT
223 updated = 1;
224 if (chmod(fname,file->mode) != 0) {
dc5ddbcc 225 fprintf(FERROR,"failed to set permissions on %s : %s\n",
c627d613 226 fname,strerror(errno));
7b8356d0 227 return 0;
c627d613
AT
228 }
229 }
230#endif
231
7b8356d0 232 if ((am_root && preserve_uid && st->st_uid != file->uid) ||
c627d613
AT
233 (preserve_gid && st->st_gid != file->gid)) {
234 updated = 1;
8bf73749
AT
235 if (lchown(fname,
236 (am_root&&preserve_uid)?file->uid:-1,
237 preserve_gid?file->gid:-1) != 0) {
c627d613 238 if (verbose>1 || preserve_uid)
dc5ddbcc 239 fprintf(FERROR,"chown %s : %s\n",fname,strerror(errno));
7b8356d0 240 return updated;
c627d613
AT
241 }
242 }
243
244 if (verbose > 1 && report) {
245 if (updated)
dc5ddbcc 246 fprintf(FINFO,"%s\n",fname);
c627d613 247 else
dc5ddbcc 248 fprintf(FINFO,"%s is uptodate\n",fname);
c627d613 249 }
7b8356d0 250 return updated;
c627d613
AT
251}
252
253
254void recv_generator(char *fname,struct file_list *flist,int i,int f_out)
255{
256 int fd;
257 struct stat st;
c6e7fcb4 258 struct map_struct *buf;
c627d613 259 struct sum_struct *s;
ebb0a6f6 260 char sum[MD4_SUM_LENGTH];
c627d613 261 int statret;
dc5ddbcc 262 struct file_struct *file = &flist->files[i];
c627d613
AT
263
264 if (verbose > 2)
9e31c482 265 fprintf(FERROR,"recv_generator(%s,%d)\n",fname,i);
c627d613
AT
266
267 statret = lstat(fname,&st);
268
7b8356d0
AT
269 if (S_ISDIR(file->mode)) {
270 if (dry_run) return;
271 if (statret == 0 && !S_ISDIR(st.st_mode)) {
272 if (unlink(fname) != 0) {
273 fprintf(FERROR,"unlink %s : %s\n",fname,strerror(errno));
274 return;
275 }
276 statret = -1;
277 }
6574b4f7
AT
278 if (statret != 0 && mkdir(fname,file->mode) != 0 && errno != EEXIST) {
279 if (!(relative_paths && errno==ENOENT &&
280 create_directory_path(fname)==0 &&
281 mkdir(fname,file->mode)==0)) {
282 fprintf(FERROR,"mkdir %s : %s (2)\n",
283 fname,strerror(errno));
284 }
285 }
7b8356d0
AT
286 if (set_perms(fname,file,NULL,0) && verbose)
287 fprintf(FINFO,"%s/\n",fname);
288 return;
289 }
290
c627d613 291#if SUPPORT_LINKS
dc5ddbcc 292 if (preserve_links && S_ISLNK(file->mode)) {
c627d613
AT
293 char lnk[MAXPATHLEN];
294 int l;
295 if (statret == 0) {
296 l = readlink(fname,lnk,MAXPATHLEN-1);
297 if (l > 0) {
298 lnk[l] = 0;
dc5ddbcc
AT
299 if (strcmp(lnk,file->link) == 0) {
300 set_perms(fname,file,&st,1);
c627d613
AT
301 return;
302 }
303 }
304 }
305 if (!dry_run) unlink(fname);
dc5ddbcc
AT
306 if (!dry_run && symlink(file->link,fname) != 0) {
307 fprintf(FERROR,"link %s -> %s : %s\n",
308 fname,file->link,strerror(errno));
c627d613 309 } else {
dc5ddbcc 310 set_perms(fname,file,NULL,0);
c627d613 311 if (verbose)
dc5ddbcc
AT
312 fprintf(FINFO,"%s -> %s\n",
313 fname,file->link);
c627d613
AT
314 }
315 return;
316 }
317#endif
318
319#ifdef HAVE_MKNOD
7b8356d0 320 if (am_root && preserve_devices && IS_DEVICE(file->mode)) {
c627d613 321 if (statret != 0 ||
dc5ddbcc
AT
322 st.st_mode != file->mode ||
323 st.st_rdev != file->rdev) {
c627d613
AT
324 if (!dry_run) unlink(fname);
325 if (verbose > 2)
dc5ddbcc
AT
326 fprintf(FERROR,"mknod(%s,0%o,0x%x)\n",
327 fname,(int)file->mode,(int)file->rdev);
c627d613 328 if (!dry_run &&
dc5ddbcc
AT
329 mknod(fname,file->mode,file->rdev) != 0) {
330 fprintf(FERROR,"mknod %s : %s\n",fname,strerror(errno));
c627d613 331 } else {
dc5ddbcc 332 set_perms(fname,file,NULL,0);
c627d613 333 if (verbose)
dc5ddbcc 334 fprintf(FINFO,"%s\n",fname);
c627d613
AT
335 }
336 } else {
dc5ddbcc 337 set_perms(fname,file,&st,1);
c627d613
AT
338 }
339 return;
340 }
341#endif
342
dc5ddbcc
AT
343 if (preserve_hard_links && check_hard_link(file)) {
344 if (verbose > 1)
345 fprintf(FINFO,"%s is a hard link\n",file->name);
346 return;
347 }
348
349 if (!S_ISREG(file->mode)) {
350 fprintf(FERROR,"skipping non-regular file %s\n",fname);
c627d613
AT
351 return;
352 }
353
354 if (statret == -1) {
355 if (errno == ENOENT) {
356 write_int(f_out,i);
357 if (!dry_run) send_sums(NULL,f_out);
358 } else {
359 if (verbose > 1)
dc5ddbcc 360 fprintf(FERROR,"recv_generator failed to open %s\n",fname);
c627d613
AT
361 }
362 return;
363 }
364
365 if (!S_ISREG(st.st_mode)) {
7b8356d0
AT
366 /* its not a regular file on the receiving end, but it is on the
367 sending end. If its a directory then skip it (too dangerous to
368 do a recursive deletion??) otherwise try to unlink it */
369 if (S_ISDIR(st.st_mode)) {
370 fprintf(FERROR,"ERROR: %s is a directory\n",fname);
371 return;
372 }
373 if (unlink(fname) != 0) {
374 fprintf(FERROR,"%s : not a regular file (generator)\n",fname);
375 return;
376 }
377
378 /* now pretend the file didn't exist */
379 write_int(f_out,i);
380 if (!dry_run) send_sums(NULL,f_out);
c627d613
AT
381 return;
382 }
383
dc5ddbcc 384 if (update_only && st.st_mtime >= file->modtime) {
c627d613 385 if (verbose > 1)
dc5ddbcc 386 fprintf(FERROR,"%s is newer\n",fname);
c627d613
AT
387 return;
388 }
389
390 if (always_checksum && S_ISREG(st.st_mode)) {
391 file_checksum(fname,sum,st.st_size);
392 }
393
dc5ddbcc
AT
394 if (st.st_size == file->length &&
395 ((!ignore_times && st.st_mtime == file->modtime) ||
c627d613 396 (always_checksum && S_ISREG(st.st_mode) &&
dc5ddbcc
AT
397 memcmp(sum,file->sum,csum_length) == 0))) {
398 set_perms(fname,file,&st,1);
c627d613
AT
399 return;
400 }
401
402 if (dry_run) {
403 write_int(f_out,i);
404 return;
405 }
406
407 /* open the file */
408 fd = open(fname,O_RDONLY);
409
410 if (fd == -1) {
dc5ddbcc 411 fprintf(FERROR,"failed to open %s : %s\n",fname,strerror(errno));
c627d613
AT
412 return;
413 }
414
415 if (st.st_size > 0) {
416 buf = map_file(fd,st.st_size);
c627d613
AT
417 } else {
418 buf = NULL;
419 }
420
421 if (verbose > 3)
9e31c482 422 fprintf(FERROR,"gen mapped %s of size %d\n",fname,(int)st.st_size);
c627d613
AT
423
424 s = generate_sums(buf,st.st_size,block_size);
425
9e31c482
AT
426 if (verbose > 2)
427 fprintf(FERROR,"sending sums for %d\n",i);
428
c627d613
AT
429 write_int(f_out,i);
430 send_sums(s,f_out);
431 write_flush(f_out);
432
433 close(fd);
1cdc8b50 434 if (buf) unmap_file(buf);
c627d613
AT
435
436 free_sums(s);
437}
438
439
440
9e31c482 441static int receive_data(int f_in,struct map_struct *buf,int fd,char *fname)
c627d613
AT
442{
443 int i,n,remainder,len,count;
444 off_t offset = 0;
445 off_t offset2;
70d794dc 446 char *data;
ebb0a6f6
AT
447 static char file_sum1[MD4_SUM_LENGTH];
448 static char file_sum2[MD4_SUM_LENGTH];
9e31c482 449 char *map=NULL;
c627d613
AT
450
451 count = read_int(f_in);
452 n = read_int(f_in);
453 remainder = read_int(f_in);
454
9e31c482
AT
455 sum_init();
456
70d794dc 457 for (i=recv_token(f_in,&data); i != 0; i=recv_token(f_in,&data)) {
c627d613
AT
458 if (i > 0) {
459 if (verbose > 3)
dc5ddbcc 460 fprintf(FERROR,"data recv %d at %d\n",i,(int)offset);
c627d613 461
9e31c482
AT
462 sum_update(data,i);
463
7b8356d0 464 if (fd != -1 && write_sparse(fd,data,i) != i) {
dc5ddbcc 465 fprintf(FERROR,"write failed on %s : %s\n",fname,strerror(errno));
34ccb63e 466 exit_cleanup(1);
c627d613
AT
467 }
468 offset += i;
469 } else {
470 i = -(i+1);
471 offset2 = i*n;
472 len = n;
473 if (i == count-1 && remainder != 0)
474 len = remainder;
475
476 if (verbose > 3)
dc5ddbcc 477 fprintf(FERROR,"chunk[%d] of size %d at %d offset=%d\n",
c627d613
AT
478 i,len,(int)offset2,(int)offset);
479
9e31c482
AT
480 map = map_ptr(buf,offset2,len);
481
861c20b4 482 see_token(map, len);
9e31c482
AT
483 sum_update(map,len);
484
7b8356d0 485 if (fd != -1 && write_sparse(fd,map,len) != len) {
dc5ddbcc 486 fprintf(FERROR,"write failed on %s : %s\n",fname,strerror(errno));
34ccb63e 487 exit_cleanup(1);
c627d613
AT
488 }
489 offset += len;
490 }
491 }
7bec6a5c 492
7b8356d0 493 if (fd != -1 && offset > 0 && sparse_end(fd) != 0) {
dc5ddbcc 494 fprintf(FERROR,"write failed on %s : %s\n",fname,strerror(errno));
34ccb63e 495 exit_cleanup(1);
7bec6a5c 496 }
9e31c482
AT
497
498 sum_end(file_sum1);
499
500 if (remote_version >= 14) {
ebb0a6f6 501 read_buf(f_in,file_sum2,MD4_SUM_LENGTH);
9e31c482
AT
502 if (verbose > 2)
503 fprintf(FERROR,"got file_sum\n");
7b8356d0 504 if (fd != -1 && memcmp(file_sum1,file_sum2,MD4_SUM_LENGTH) != 0)
9e31c482
AT
505 return 0;
506 }
507 return 1;
c627d613
AT
508}
509
510
511static void delete_one(struct file_struct *f)
512{
513 if (!S_ISDIR(f->mode)) {
514 if (!dry_run && unlink(f->name) != 0) {
dc5ddbcc 515 fprintf(FERROR,"unlink %s : %s\n",f->name,strerror(errno));
c627d613 516 } else if (verbose) {
dc5ddbcc 517 fprintf(FERROR,"deleting %s\n",f->name);
c627d613
AT
518 }
519 } else {
520 if (!dry_run && rmdir(f->name) != 0) {
521 if (errno != ENOTEMPTY)
dc5ddbcc 522 fprintf(FERROR,"rmdir %s : %s\n",f->name,strerror(errno));
c627d613 523 } else if (verbose) {
dc5ddbcc 524 fprintf(FERROR,"deleting directory %s\n",f->name);
c627d613
AT
525 }
526 }
527}
528
529
530static void delete_files(struct file_list *flist)
531{
532 struct file_list *local_file_list;
533 char *dot=".";
534 int i;
535
4fe159a8 536 if (cvs_exclude)
79fbb6f5 537 add_cvs_excludes();
4fe159a8 538
a06d19e3 539 if (!(local_file_list = send_file_list(-1,1,&dot)))
c627d613
AT
540 return;
541
774ef68f 542 for (i=local_file_list->count-1;i>=0;i--) {
c627d613
AT
543 if (!local_file_list->files[i].name) continue;
544 if (-1 == flist_find(flist,&local_file_list->files[i])) {
545 delete_one(&local_file_list->files[i]);
546 }
547 }
548}
549
550static char *cleanup_fname = NULL;
551
34ccb63e 552void exit_cleanup(int code)
c627d613
AT
553{
554 if (cleanup_fname)
555 unlink(cleanup_fname);
34ccb63e
AT
556 exit(code);
557}
558
559void sig_int(void)
560{
561 exit_cleanup(1);
c627d613
AT
562}
563
564
c6e7fcb4 565int recv_files(int f_in,struct file_list *flist,char *local_name,int f_gen)
c627d613
AT
566{
567 int fd1,fd2;
568 struct stat st;
569 char *fname;
570 char fnametmp[MAXPATHLEN];
c6e7fcb4 571 struct map_struct *buf;
c627d613 572 int i;
dc5ddbcc 573 struct file_struct *file;
c6e7fcb4 574 int phase=0;
9e31c482 575 int recv_ok;
c627d613 576
981791bd 577 if (verbose > 2) {
dc5ddbcc 578 fprintf(FERROR,"recv_files(%d) starting\n",flist->count);
981791bd 579 }
c627d613
AT
580
581 if (recurse && delete_mode && !local_name && flist->count>0) {
582 delete_files(flist);
583 }
584
585 while (1)
9e31c482 586 {
c627d613 587 i = read_int(f_in);
c6e7fcb4
AT
588 if (i == -1) {
589 if (phase==0 && remote_version >= 13) {
590 phase++;
9e31c482
AT
591 csum_length = SUM_LENGTH;
592 if (verbose > 2)
593 fprintf(FERROR,"recv_files phase=%d\n",phase);
c6e7fcb4
AT
594 write_int(f_gen,-1);
595 write_flush(f_gen);
596 continue;
597 }
598 break;
599 }
c627d613 600
dc5ddbcc
AT
601 file = &flist->files[i];
602 fname = file->name;
c627d613
AT
603
604 if (local_name)
605 fname = local_name;
606
607 if (dry_run) {
608 if (!am_server && verbose)
609 printf("%s\n",fname);
610 continue;
611 }
612
613 if (verbose > 2)
dc5ddbcc 614 fprintf(FERROR,"recv_files(%s)\n",fname);
c627d613
AT
615
616 /* open the file */
ac1eb754 617 fd1 = open(fname,O_RDONLY);
c627d613 618
ac1eb754 619 if (fd1 != -1 && fstat(fd1,&st) != 0) {
dc5ddbcc 620 fprintf(FERROR,"fstat %s : %s\n",fname,strerror(errno));
7b8356d0 621 receive_data(f_in,NULL,-1,NULL);
c627d613 622 close(fd1);
7b8356d0 623 continue;
c627d613
AT
624 }
625
ac1eb754 626 if (fd1 != -1 && !S_ISREG(st.st_mode)) {
7b8356d0
AT
627 fprintf(FERROR,"%s : not a regular file (recv_files)\n",fname);
628 receive_data(f_in,NULL,-1,NULL);
c627d613 629 close(fd1);
7b8356d0 630 continue;
c627d613
AT
631 }
632
ac1eb754 633 if (fd1 != -1 && st.st_size > 0) {
c627d613 634 buf = map_file(fd1,st.st_size);
9e31c482
AT
635 if (verbose > 2)
636 fprintf(FERROR,"recv mapped %s of size %d\n",fname,(int)st.st_size);
c627d613
AT
637 } else {
638 buf = NULL;
639 }
640
c627d613
AT
641 /* open tmp file */
642 sprintf(fnametmp,"%s.XXXXXX",fname);
643 if (NULL == mktemp(fnametmp)) {
dc5ddbcc 644 fprintf(FERROR,"mktemp %s failed\n",fnametmp);
7b8356d0
AT
645 receive_data(f_in,buf,-1,NULL);
646 if (buf) unmap_file(buf);
647 close(fd1);
648 continue;
c627d613 649 }
dc5ddbcc 650 fd2 = open(fnametmp,O_WRONLY|O_CREAT,file->mode);
6574b4f7
AT
651 if (relative_paths && errno == ENOENT &&
652 create_directory_path(fnametmp) == 0) {
653 fd2 = open(fnametmp,O_WRONLY|O_CREAT,file->mode);
654 }
c627d613 655 if (fd2 == -1) {
dc5ddbcc 656 fprintf(FERROR,"open %s : %s\n",fnametmp,strerror(errno));
7b8356d0
AT
657 receive_data(f_in,buf,-1,NULL);
658 if (buf) unmap_file(buf);
659 close(fd1);
660 continue;
c627d613
AT
661 }
662
663 cleanup_fname = fnametmp;
664
665 if (!am_server && verbose)
666 printf("%s\n",fname);
667
668 /* recv file data */
9e31c482 669 recv_ok = receive_data(f_in,buf,fd2,fname);
c627d613 670
981791bd 671 if (fd1 != -1) {
1cdc8b50 672 if (buf) unmap_file(buf);
981791bd
AT
673 close(fd1);
674 }
c627d613
AT
675 close(fd2);
676
677 if (verbose > 2)
dc5ddbcc 678 fprintf(FERROR,"renaming %s to %s\n",fnametmp,fname);
c627d613
AT
679
680 if (make_backups) {
681 char fnamebak[MAXPATHLEN];
682 sprintf(fnamebak,"%s%s",fname,backup_suffix);
ac1eb754 683 if (rename(fname,fnamebak) != 0 && errno != ENOENT) {
dc5ddbcc 684 fprintf(FERROR,"rename %s %s : %s\n",fname,fnamebak,strerror(errno));
7b8356d0 685 continue;
c627d613
AT
686 }
687 }
688
689 /* move tmp file over real file */
690 if (rename(fnametmp,fname) != 0) {
dc5ddbcc 691 fprintf(FERROR,"rename %s -> %s : %s\n",
c627d613 692 fnametmp,fname,strerror(errno));
7b8356d0 693 unlink(fnametmp);
c627d613
AT
694 }
695
696 cleanup_fname = NULL;
697
dc5ddbcc 698 set_perms(fname,file,NULL,0);
9e31c482
AT
699
700 if (!recv_ok) {
701 if (verbose > 1)
702 fprintf(FERROR,"redoing %s(%d)\n",fname,i);
52296954
AT
703 if (csum_length == SUM_LENGTH)
704 fprintf(FERROR,"ERROR: file corruption in %s\n",fname);
9e31c482
AT
705 write_int(f_gen,i);
706 }
c627d613
AT
707 }
708
7b8356d0
AT
709 /* now we need to fix any directory permissions that were
710 modified during the transfer */
711 if (!am_root) {
712 for (i = 0; i < flist->count; i++) {
713 struct file_struct *file = &flist->files[i];
714 if (!file->name || !S_ISDIR(file->mode)) continue;
715 recv_generator(file->name,flist,i,-1);
716 }
717 }
718
719
c627d613 720 if (verbose > 2)
dc5ddbcc 721 fprintf(FERROR,"recv_files finished\n");
c627d613
AT
722
723 return 0;
724}
725
726
727
728off_t send_files(struct file_list *flist,int f_out,int f_in)
729{
730 int fd;
731 struct sum_struct *s;
c6e7fcb4 732 struct map_struct *buf;
c627d613
AT
733 struct stat st;
734 char fname[MAXPATHLEN];
735 off_t total=0;
736 int i;
dc5ddbcc 737 struct file_struct *file;
c6e7fcb4 738 int phase = 0;
c627d613
AT
739
740 if (verbose > 2)
dc5ddbcc 741 fprintf(FERROR,"send_files starting\n");
c627d613 742
720b47f2
AT
743 setup_nonblocking(f_in,f_out);
744
c627d613
AT
745 while (1)
746 {
747 i = read_int(f_in);
c6e7fcb4
AT
748 if (i == -1) {
749 if (phase==0 && remote_version >= 13) {
750 phase++;
9e31c482 751 csum_length = SUM_LENGTH;
c6e7fcb4
AT
752 write_int(f_out,-1);
753 write_flush(f_out);
9e31c482
AT
754 if (verbose > 2)
755 fprintf(FERROR,"send_files phase=%d\n",phase);
c6e7fcb4
AT
756 continue;
757 }
758 break;
759 }
c627d613 760
dc5ddbcc
AT
761 file = &flist->files[i];
762
c627d613 763 fname[0] = 0;
dc5ddbcc
AT
764 if (file->dir) {
765 strcpy(fname,file->dir);
c627d613
AT
766 strcat(fname,"/");
767 }
dc5ddbcc 768 strcat(fname,file->name);
c627d613
AT
769
770 if (verbose > 2)
dc5ddbcc 771 fprintf(FERROR,"send_files(%d,%s)\n",i,fname);
c627d613
AT
772
773 if (dry_run) {
774 if (!am_server && verbose)
775 printf("%s\n",fname);
776 write_int(f_out,i);
777 continue;
778 }
779
780 s = receive_sums(f_in);
781 if (!s) {
dc5ddbcc 782 fprintf(FERROR,"receive_sums failed\n");
c627d613
AT
783 return -1;
784 }
785
786 fd = open(fname,O_RDONLY);
787 if (fd == -1) {
dc5ddbcc 788 fprintf(FERROR,"send_files failed to open %s: %s\n",
c627d613
AT
789 fname,strerror(errno));
790 continue;
791 }
792
793 /* map the local file */
794 if (fstat(fd,&st) != 0) {
dc5ddbcc 795 fprintf(FERROR,"fstat failed : %s\n",strerror(errno));
c627d613
AT
796 return -1;
797 }
798
799 if (st.st_size > 0) {
800 buf = map_file(fd,st.st_size);
c627d613
AT
801 } else {
802 buf = NULL;
803 }
804
805 if (verbose > 2)
dc5ddbcc 806 fprintf(FERROR,"send_files mapped %s of size %d\n",
c627d613
AT
807 fname,(int)st.st_size);
808
809 write_int(f_out,i);
810
811 write_int(f_out,s->count);
812 write_int(f_out,s->n);
813 write_int(f_out,s->remainder);
814
815 if (verbose > 2)
dc5ddbcc 816 fprintf(FERROR,"calling match_sums %s\n",fname);
c627d613
AT
817
818 if (!am_server && verbose)
819 printf("%s\n",fname);
820
720b47f2 821 match_sums(f_out,s,buf,st.st_size);
c627d613
AT
822 write_flush(f_out);
823
1cdc8b50 824 if (buf) unmap_file(buf);
c627d613
AT
825 close(fd);
826
827 free_sums(s);
828
829 if (verbose > 2)
dc5ddbcc 830 fprintf(FERROR,"sender finished %s\n",fname);
c627d613
AT
831
832 total += st.st_size;
833 }
834
dc5ddbcc
AT
835 if (verbose > 2)
836 fprintf(FERROR,"send files finished\n");
837
c627d613
AT
838 match_report();
839
840 write_int(f_out,-1);
841 write_flush(f_out);
842
843 return total;
844}
845
846
847
c6e7fcb4 848void generate_files(int f,struct file_list *flist,char *local_name,int f_recv)
c627d613
AT
849{
850 int i;
9e31c482 851 int phase=0;
c627d613
AT
852
853 if (verbose > 2)
dc5ddbcc 854 fprintf(FERROR,"generator starting pid=%d count=%d\n",
c627d613
AT
855 (int)getpid(),flist->count);
856
857 for (i = 0; i < flist->count; i++) {
dc5ddbcc 858 struct file_struct *file = &flist->files[i];
7b8356d0 859 mode_t saved_mode = file->mode;
dc5ddbcc 860 if (!file->name) continue;
7b8356d0
AT
861
862 /* we need to ensure that any directories we create have writeable
863 permissions initially so that we can create the files within
864 them. This is then fixed after the files are transferred */
865 if (!am_root && S_ISDIR(file->mode)) {
866 file->mode |= S_IWUSR; /* user write */
c627d613 867 }
7b8356d0 868
dc5ddbcc 869 recv_generator(local_name?local_name:file->name,
c627d613 870 flist,i,f);
7b8356d0
AT
871
872 file->mode = saved_mode;
c627d613 873 }
c6e7fcb4 874
9e31c482
AT
875 phase++;
876 csum_length = SUM_LENGTH;
877 ignore_times=1;
878
879 if (verbose > 2)
880 fprintf(FERROR,"generate_files phase=%d\n",phase);
881
c627d613
AT
882 write_int(f,-1);
883 write_flush(f);
c6e7fcb4
AT
884
885 if (remote_version >= 13) {
7b8356d0
AT
886 /* in newer versions of the protocol the files can cycle through
887 the system more than once to catch initial checksum errors */
c6e7fcb4
AT
888 for (i=read_int(f_recv); i != -1; i=read_int(f_recv)) {
889 struct file_struct *file = &flist->files[i];
890 recv_generator(local_name?local_name:file->name,
891 flist,i,f);
892 }
893
9e31c482
AT
894 phase++;
895 if (verbose > 2)
896 fprintf(FERROR,"generate_files phase=%d\n",phase);
897
c6e7fcb4
AT
898 write_int(f,-1);
899 write_flush(f);
900 }
901
902
c627d613 903 if (verbose > 2)
dc5ddbcc 904 fprintf(FERROR,"generator wrote %d\n",write_total());
c627d613 905}
dc5ddbcc
AT
906
907