changes to make VPATH work correctly, thanks to patl@cag.lcs.mit.edu
[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 31extern char *backup_suffix;
950ab32d 32extern char *tmpdir;
c627d613 33
82306bf6 34extern int whole_file;
c627d613
AT
35extern int block_size;
36extern int update_only;
37extern int make_backups;
38extern int preserve_links;
dc5ddbcc 39extern int preserve_hard_links;
c627d613
AT
40extern int preserve_perms;
41extern int preserve_devices;
42extern int preserve_uid;
43extern int preserve_gid;
44extern int preserve_times;
45extern int dry_run;
46extern int ignore_times;
47extern int recurse;
48extern int delete_mode;
49extern int cvs_exclude;
7b8356d0 50extern int am_root;
6574b4f7 51extern int relative_paths;
c627d613
AT
52
53/*
54 free a sums struct
55 */
56static void free_sums(struct sum_struct *s)
57{
58 if (s->sums) free(s->sums);
59 free(s);
60}
61
62
63
64/*
65 send a sums struct down a fd
66 */
67static void send_sums(struct sum_struct *s,int f_out)
68{
69 int i;
70
71 /* tell the other guy how many we are going to be doing and how many
72 bytes there are in the last chunk */
73 write_int(f_out,s?s->count:0);
74 write_int(f_out,s?s->n:block_size);
75 write_int(f_out,s?s->remainder:0);
76 if (s)
77 for (i=0;i<s->count;i++) {
78 write_int(f_out,s->sums[i].sum1);
43a481dc 79 write_buf(f_out,s->sums[i].sum2,csum_length);
c627d613
AT
80 }
81 write_flush(f_out);
82}
83
84
85/*
86 generate a stream of signatures/checksums that describe a buffer
87
88 generate approximately one checksum every n bytes
89 */
c6e7fcb4 90static struct sum_struct *generate_sums(struct map_struct *buf,off_t len,int n)
c627d613
AT
91{
92 int i;
93 struct sum_struct *s;
94 int count;
95 int block_len = n;
96 int remainder = (len%block_len);
97 off_t offset = 0;
98
99 count = (len+(block_len-1))/block_len;
100
101 s = (struct sum_struct *)malloc(sizeof(*s));
102 if (!s) out_of_memory("generate_sums");
103
104 s->count = count;
105 s->remainder = remainder;
106 s->n = n;
107 s->flength = len;
108
109 if (count==0) {
110 s->sums = NULL;
111 return s;
112 }
113
114 if (verbose > 3)
dc5ddbcc 115 fprintf(FERROR,"count=%d rem=%d n=%d flength=%d\n",
c627d613
AT
116 s->count,s->remainder,s->n,(int)s->flength);
117
118 s->sums = (struct sum_buf *)malloc(sizeof(s->sums[0])*s->count);
119 if (!s->sums) out_of_memory("generate_sums");
120
121 for (i=0;i<count;i++) {
122 int n1 = MIN(len,n);
d9bea2dd 123 char *map = map_ptr(buf,offset,n1);
c627d613 124
d9bea2dd
AT
125 s->sums[i].sum1 = get_checksum1(map,n1);
126 get_checksum2(map,n1,s->sums[i].sum2);
c627d613
AT
127
128 s->sums[i].offset = offset;
129 s->sums[i].len = n1;
130 s->sums[i].i = i;
131
132 if (verbose > 3)
dc5ddbcc 133 fprintf(FERROR,"chunk[%d] offset=%d len=%d sum1=%08x\n",
c627d613
AT
134 i,(int)s->sums[i].offset,s->sums[i].len,s->sums[i].sum1);
135
136 len -= n1;
c627d613
AT
137 offset += n1;
138 }
139
140 return s;
141}
142
143
144/*
145 receive the checksums for a buffer
146 */
147static struct sum_struct *receive_sums(int f)
148{
149 struct sum_struct *s;
150 int i;
151 off_t offset = 0;
c627d613
AT
152
153 s = (struct sum_struct *)malloc(sizeof(*s));
154 if (!s) out_of_memory("receive_sums");
155
156 s->count = read_int(f);
157 s->n = read_int(f);
158 s->remainder = read_int(f);
159 s->sums = NULL;
160
161 if (verbose > 3)
dc5ddbcc 162 fprintf(FERROR,"count=%d n=%d rem=%d\n",
c627d613
AT
163 s->count,s->n,s->remainder);
164
c627d613
AT
165 if (s->count == 0)
166 return(s);
167
168 s->sums = (struct sum_buf *)malloc(sizeof(s->sums[0])*s->count);
169 if (!s->sums) out_of_memory("receive_sums");
170
171 for (i=0;i<s->count;i++) {
172 s->sums[i].sum1 = read_int(f);
43a481dc 173 read_buf(f,s->sums[i].sum2,csum_length);
c627d613
AT
174
175 s->sums[i].offset = offset;
176 s->sums[i].i = i;
177
178 if (i == s->count-1 && s->remainder != 0) {
179 s->sums[i].len = s->remainder;
180 } else {
181 s->sums[i].len = s->n;
182 }
183 offset += s->sums[i].len;
184
185 if (verbose > 3)
dc5ddbcc 186 fprintf(FERROR,"chunk[%d] len=%d offset=%d sum1=%08x\n",
c627d613
AT
187 i,s->sums[i].len,(int)s->sums[i].offset,s->sums[i].sum1);
188 }
189
190 s->flength = offset;
191
192 return s;
193}
194
195
7b8356d0
AT
196static int set_perms(char *fname,struct file_struct *file,struct stat *st,
197 int report)
c627d613
AT
198{
199 int updated = 0;
200 struct stat st2;
201
7b8356d0 202 if (dry_run) return 0;
c627d613
AT
203
204 if (!st) {
82306bf6 205 if (link_stat(fname,&st2) != 0) {
dc5ddbcc 206 fprintf(FERROR,"stat %s : %s\n",fname,strerror(errno));
7b8356d0 207 return 0;
c627d613
AT
208 }
209 st = &st2;
210 }
211
7bec6a5c
AT
212 if (preserve_times && !S_ISLNK(st->st_mode) &&
213 st->st_mtime != file->modtime) {
c627d613
AT
214 updated = 1;
215 if (set_modtime(fname,file->modtime) != 0) {
dc5ddbcc 216 fprintf(FERROR,"failed to set times on %s : %s\n",
c627d613 217 fname,strerror(errno));
7b8356d0 218 return 0;
c627d613
AT
219 }
220 }
221
222#ifdef HAVE_CHMOD
7bec6a5c
AT
223 if (preserve_perms && !S_ISLNK(st->st_mode) &&
224 st->st_mode != file->mode) {
c627d613 225 updated = 1;
7308bd66 226 if (do_chmod(fname,file->mode) != 0) {
dc5ddbcc 227 fprintf(FERROR,"failed to set permissions on %s : %s\n",
c627d613 228 fname,strerror(errno));
7b8356d0 229 return 0;
c627d613
AT
230 }
231 }
232#endif
233
7b8356d0 234 if ((am_root && preserve_uid && st->st_uid != file->uid) ||
c627d613
AT
235 (preserve_gid && st->st_gid != file->gid)) {
236 updated = 1;
7308bd66 237 if (do_lchown(fname,
8bf73749
AT
238 (am_root&&preserve_uid)?file->uid:-1,
239 preserve_gid?file->gid:-1) != 0) {
c627d613 240 if (verbose>1 || preserve_uid)
dc5ddbcc 241 fprintf(FERROR,"chown %s : %s\n",fname,strerror(errno));
7b8356d0 242 return updated;
c627d613
AT
243 }
244 }
245
246 if (verbose > 1 && report) {
247 if (updated)
dc5ddbcc 248 fprintf(FINFO,"%s\n",fname);
c627d613 249 else
dc5ddbcc 250 fprintf(FINFO,"%s is uptodate\n",fname);
c627d613 251 }
7b8356d0 252 return updated;
c627d613
AT
253}
254
255
964ca2ec
AT
256/* choose whether to skip a particular file */
257static int skip_file(char *fname,
258 struct file_struct *file, struct stat *st)
259{
260 if (st->st_size != file->length) {
261 return 0;
262 }
263
264 /* if always checksum is set then we use the checksum instead
265 of the file time to determine whether to sync */
266 if (always_checksum && S_ISREG(st->st_mode)) {
267 char sum[MD4_SUM_LENGTH];
268 file_checksum(fname,sum,st->st_size);
269 return (memcmp(sum,file->sum,csum_length) == 0);
270 }
271
272 if (ignore_times) {
273 return 0;
274 }
275
276 return (st->st_mtime == file->modtime);
277}
278
279
3ba62a83
AT
280/* use a larger block size for really big files */
281int adapt_block_size(struct file_struct *file, int bsize)
282{
283 int ret = file->length / (10000); /* rough heuristic */
284 ret = ret & ~15; /* multiple of 16 */
285 if (ret < bsize) ret = bsize;
1aa71c8d 286 if (ret > CHUNK_SIZE/2) ret = CHUNK_SIZE/2;
3ba62a83
AT
287 return ret;
288}
289
c627d613
AT
290void recv_generator(char *fname,struct file_list *flist,int i,int f_out)
291{
292 int fd;
293 struct stat st;
c6e7fcb4 294 struct map_struct *buf;
c627d613 295 struct sum_struct *s;
c627d613 296 int statret;
3ec4dd97 297 struct file_struct *file = flist->files[i];
c627d613
AT
298
299 if (verbose > 2)
9e31c482 300 fprintf(FERROR,"recv_generator(%s,%d)\n",fname,i);
c627d613 301
82306bf6 302 statret = link_stat(fname,&st);
c627d613 303
7b8356d0
AT
304 if (S_ISDIR(file->mode)) {
305 if (dry_run) return;
306 if (statret == 0 && !S_ISDIR(st.st_mode)) {
31e12522 307 if (do_unlink(fname) != 0) {
7b8356d0
AT
308 fprintf(FERROR,"unlink %s : %s\n",fname,strerror(errno));
309 return;
310 }
311 statret = -1;
312 }
6574b4f7
AT
313 if (statret != 0 && mkdir(fname,file->mode) != 0 && errno != EEXIST) {
314 if (!(relative_paths && errno==ENOENT &&
315 create_directory_path(fname)==0 &&
316 mkdir(fname,file->mode)==0)) {
317 fprintf(FERROR,"mkdir %s : %s (2)\n",
318 fname,strerror(errno));
319 }
320 }
7b8356d0
AT
321 if (set_perms(fname,file,NULL,0) && verbose)
322 fprintf(FINFO,"%s/\n",fname);
323 return;
324 }
325
dc5ddbcc 326 if (preserve_links && S_ISLNK(file->mode)) {
cbbe4892 327#if SUPPORT_LINKS
c627d613
AT
328 char lnk[MAXPATHLEN];
329 int l;
330 if (statret == 0) {
331 l = readlink(fname,lnk,MAXPATHLEN-1);
332 if (l > 0) {
333 lnk[l] = 0;
dc5ddbcc
AT
334 if (strcmp(lnk,file->link) == 0) {
335 set_perms(fname,file,&st,1);
c627d613
AT
336 return;
337 }
338 }
339 }
31e12522
AT
340 do_unlink(fname);
341 if (do_symlink(file->link,fname) != 0) {
dc5ddbcc
AT
342 fprintf(FERROR,"link %s -> %s : %s\n",
343 fname,file->link,strerror(errno));
c627d613 344 } else {
dc5ddbcc 345 set_perms(fname,file,NULL,0);
c627d613 346 if (verbose)
dc5ddbcc
AT
347 fprintf(FINFO,"%s -> %s\n",
348 fname,file->link);
c627d613 349 }
cbbe4892 350#endif
c627d613
AT
351 return;
352 }
c627d613
AT
353
354#ifdef HAVE_MKNOD
7b8356d0 355 if (am_root && preserve_devices && IS_DEVICE(file->mode)) {
c627d613 356 if (statret != 0 ||
dc5ddbcc
AT
357 st.st_mode != file->mode ||
358 st.st_rdev != file->rdev) {
31e12522 359 do_unlink(fname);
c627d613 360 if (verbose > 2)
dc5ddbcc
AT
361 fprintf(FERROR,"mknod(%s,0%o,0x%x)\n",
362 fname,(int)file->mode,(int)file->rdev);
31e12522 363 if (do_mknod(fname,file->mode,file->rdev) != 0) {
dc5ddbcc 364 fprintf(FERROR,"mknod %s : %s\n",fname,strerror(errno));
c627d613 365 } else {
dc5ddbcc 366 set_perms(fname,file,NULL,0);
c627d613 367 if (verbose)
dc5ddbcc 368 fprintf(FINFO,"%s\n",fname);
c627d613
AT
369 }
370 } else {
dc5ddbcc 371 set_perms(fname,file,&st,1);
c627d613
AT
372 }
373 return;
374 }
375#endif
376
dc5ddbcc
AT
377 if (preserve_hard_links && check_hard_link(file)) {
378 if (verbose > 1)
3ec4dd97 379 fprintf(FINFO,"%s is a hard link\n",f_name(file));
dc5ddbcc
AT
380 return;
381 }
382
383 if (!S_ISREG(file->mode)) {
384 fprintf(FERROR,"skipping non-regular file %s\n",fname);
c627d613
AT
385 return;
386 }
387
388 if (statret == -1) {
389 if (errno == ENOENT) {
390 write_int(f_out,i);
391 if (!dry_run) send_sums(NULL,f_out);
392 } else {
393 if (verbose > 1)
dc5ddbcc 394 fprintf(FERROR,"recv_generator failed to open %s\n",fname);
c627d613
AT
395 }
396 return;
397 }
398
399 if (!S_ISREG(st.st_mode)) {
7b8356d0
AT
400 /* its not a regular file on the receiving end, but it is on the
401 sending end. If its a directory then skip it (too dangerous to
402 do a recursive deletion??) otherwise try to unlink it */
403 if (S_ISDIR(st.st_mode)) {
404 fprintf(FERROR,"ERROR: %s is a directory\n",fname);
405 return;
406 }
31e12522 407 if (do_unlink(fname) != 0) {
7b8356d0
AT
408 fprintf(FERROR,"%s : not a regular file (generator)\n",fname);
409 return;
410 }
411
412 /* now pretend the file didn't exist */
413 write_int(f_out,i);
414 if (!dry_run) send_sums(NULL,f_out);
c627d613
AT
415 return;
416 }
417
6bbbc08b 418 if (update_only && st.st_mtime > file->modtime) {
c627d613 419 if (verbose > 1)
dc5ddbcc 420 fprintf(FERROR,"%s is newer\n",fname);
c627d613
AT
421 return;
422 }
423
964ca2ec 424 if (skip_file(fname, file, &st)) {
dc5ddbcc 425 set_perms(fname,file,&st,1);
c627d613
AT
426 return;
427 }
428
429 if (dry_run) {
430 write_int(f_out,i);
431 return;
432 }
433
82306bf6
AT
434 if (whole_file) {
435 write_int(f_out,i);
436 send_sums(NULL,f_out);
437 return;
438 }
439
c627d613
AT
440 /* open the file */
441 fd = open(fname,O_RDONLY);
442
443 if (fd == -1) {
dc5ddbcc 444 fprintf(FERROR,"failed to open %s : %s\n",fname,strerror(errno));
f3644f1f 445 fprintf(FERROR,"skipping %s\n",fname);
c627d613
AT
446 return;
447 }
448
449 if (st.st_size > 0) {
450 buf = map_file(fd,st.st_size);
c627d613
AT
451 } else {
452 buf = NULL;
453 }
454
455 if (verbose > 3)
9e31c482 456 fprintf(FERROR,"gen mapped %s of size %d\n",fname,(int)st.st_size);
c627d613 457
3ba62a83 458 s = generate_sums(buf,st.st_size,adapt_block_size(file, block_size));
c627d613 459
9e31c482
AT
460 if (verbose > 2)
461 fprintf(FERROR,"sending sums for %d\n",i);
462
c627d613
AT
463 write_int(f_out,i);
464 send_sums(s,f_out);
465 write_flush(f_out);
466
467 close(fd);
1cdc8b50 468 if (buf) unmap_file(buf);
c627d613
AT
469
470 free_sums(s);
471}
472
473
474
9e31c482 475static int receive_data(int f_in,struct map_struct *buf,int fd,char *fname)
c627d613
AT
476{
477 int i,n,remainder,len,count;
478 off_t offset = 0;
479 off_t offset2;
70d794dc 480 char *data;
ebb0a6f6
AT
481 static char file_sum1[MD4_SUM_LENGTH];
482 static char file_sum2[MD4_SUM_LENGTH];
9e31c482 483 char *map=NULL;
c627d613
AT
484
485 count = read_int(f_in);
486 n = read_int(f_in);
487 remainder = read_int(f_in);
488
9e31c482
AT
489 sum_init();
490
70d794dc 491 for (i=recv_token(f_in,&data); i != 0; i=recv_token(f_in,&data)) {
c627d613
AT
492 if (i > 0) {
493 if (verbose > 3)
dc5ddbcc 494 fprintf(FERROR,"data recv %d at %d\n",i,(int)offset);
c627d613 495
9e31c482
AT
496 sum_update(data,i);
497
7b8356d0 498 if (fd != -1 && write_sparse(fd,data,i) != i) {
dc5ddbcc 499 fprintf(FERROR,"write failed on %s : %s\n",fname,strerror(errno));
34ccb63e 500 exit_cleanup(1);
c627d613
AT
501 }
502 offset += i;
503 } else {
504 i = -(i+1);
505 offset2 = i*n;
506 len = n;
507 if (i == count-1 && remainder != 0)
508 len = remainder;
509
510 if (verbose > 3)
dc5ddbcc 511 fprintf(FERROR,"chunk[%d] of size %d at %d offset=%d\n",
c627d613
AT
512 i,len,(int)offset2,(int)offset);
513
9e31c482
AT
514 map = map_ptr(buf,offset2,len);
515
861c20b4 516 see_token(map, len);
9e31c482
AT
517 sum_update(map,len);
518
7b8356d0 519 if (fd != -1 && write_sparse(fd,map,len) != len) {
dc5ddbcc 520 fprintf(FERROR,"write failed on %s : %s\n",fname,strerror(errno));
34ccb63e 521 exit_cleanup(1);
c627d613
AT
522 }
523 offset += len;
524 }
525 }
7bec6a5c 526
7b8356d0 527 if (fd != -1 && offset > 0 && sparse_end(fd) != 0) {
dc5ddbcc 528 fprintf(FERROR,"write failed on %s : %s\n",fname,strerror(errno));
34ccb63e 529 exit_cleanup(1);
7bec6a5c 530 }
9e31c482
AT
531
532 sum_end(file_sum1);
533
534 if (remote_version >= 14) {
ebb0a6f6 535 read_buf(f_in,file_sum2,MD4_SUM_LENGTH);
9e31c482
AT
536 if (verbose > 2)
537 fprintf(FERROR,"got file_sum\n");
7b8356d0 538 if (fd != -1 && memcmp(file_sum1,file_sum2,MD4_SUM_LENGTH) != 0)
9e31c482
AT
539 return 0;
540 }
541 return 1;
c627d613
AT
542}
543
544
545static void delete_one(struct file_struct *f)
546{
547 if (!S_ISDIR(f->mode)) {
31e12522 548 if (do_unlink(f_name(f)) != 0) {
3ec4dd97 549 fprintf(FERROR,"unlink %s : %s\n",f_name(f),strerror(errno));
c627d613 550 } else if (verbose) {
3ec4dd97 551 fprintf(FERROR,"deleting %s\n",f_name(f));
c627d613
AT
552 }
553 } else {
31e12522 554 if (do_rmdir(f_name(f)) != 0) {
c627d613 555 if (errno != ENOTEMPTY)
3ec4dd97 556 fprintf(FERROR,"rmdir %s : %s\n",f_name(f),strerror(errno));
c627d613 557 } else if (verbose) {
3ec4dd97 558 fprintf(FERROR,"deleting directory %s\n",f_name(f));
c627d613
AT
559 }
560 }
561}
562
563
3b3a2fbc
AT
564/* yuck! This function wouldn't have been necessary if I had the sorting
565 algorithm right. Unfortunately fixing the sorting algorithm would introduce
566 a backward incompatibility as file list indexes are sent over the link.
567
568 The aim is to see if a directory has already had the deletion algorithm applied
569 to it (due to recursion), and if so to skip it. The bisection is to
570 prevent this being an n^2 algorithm */
571static int delete_already_done(struct file_list *flist,int j)
572{
573 int low=0,high=j-1;
574 char *name;
575 char *p;
576
577 if (j == 0) return 0;
578
3ec4dd97 579 name = strdup(f_name(flist->files[j]));
3b3a2fbc
AT
580
581 if (!name) {
582 fprintf(FERROR,"out of memory in delete_already_done");
583 exit_cleanup(1);
584 }
585
586 p = strrchr(name,'/');
587 if (!p) {
588 free(name);
589 return 0;
590 }
591 *p = 0;
592
593 while (low != high) {
594 int mid = (low+high)/2;
3ec4dd97 595 int ret = strcmp(f_name(flist->files[flist_up(flist, mid)]),name);
3b3a2fbc
AT
596 if (ret == 0) {
597 free(name);
598 return 1;
599 }
600 if (ret > 0) {
601 high=mid;
602 } else {
603 low=mid+1;
604 }
605 }
606
607 low = flist_up(flist, low);
608
3ec4dd97 609 if (strcmp(f_name(flist->files[low]),name) == 0) {
3b3a2fbc
AT
610 free(name);
611 return 1;
612 }
613
614 free(name);
615 return 0;
616}
617
618
e92338c8
AT
619/* this deletes any files on the receiving side that are not present
620 on the sending side. For version 1.6.4 I have changed the behaviour
621 to match more closely what most people seem to expect of this option */
c627d613
AT
622static void delete_files(struct file_list *flist)
623{
624 struct file_list *local_file_list;
e92338c8 625 int i, j;
c627d613 626
4fe159a8 627 if (cvs_exclude)
79fbb6f5 628 add_cvs_excludes();
4fe159a8 629
e92338c8 630 for (j=0;j<flist->count;j++) {
3ec4dd97 631 char *name = f_name(flist->files[j]);
3b3a2fbc 632
3ec4dd97 633 if (!S_ISDIR(flist->files[j]->mode)) continue;
3b3a2fbc
AT
634
635 if (delete_already_done(flist, j)) continue;
636
637 if (!(local_file_list = send_file_list(-1,1,&name)))
d966ee25 638 continue;
3b3a2fbc 639
e92338c8 640 if (verbose > 1)
3b3a2fbc 641 fprintf(FINFO,"deleting in %s\n", name);
e92338c8
AT
642
643 for (i=local_file_list->count-1;i>=0;i--) {
3ec4dd97
AT
644 if (!local_file_list->files[i]->basename) continue;
645 if (-1 == flist_find(flist,local_file_list->files[i])) {
646 delete_one(local_file_list->files[i]);
e92338c8
AT
647 }
648 }
3ec4dd97 649 flist_free(local_file_list);
c627d613
AT
650 }
651}
652
3a6a366f 653static char *cleanup_fname;
c627d613 654
34ccb63e 655void exit_cleanup(int code)
c627d613 656{
82306bf6 657 if (cleanup_fname)
31e12522 658 do_unlink(cleanup_fname);
82306bf6
AT
659 signal(SIGUSR1, SIG_IGN);
660 if (code) {
3ba62a83 661 kill_all(SIGUSR1);
82306bf6
AT
662 }
663 exit(code);
34ccb63e
AT
664}
665
666void sig_int(void)
667{
668 exit_cleanup(1);
c627d613
AT
669}
670
671
c6e7fcb4 672int recv_files(int f_in,struct file_list *flist,char *local_name,int f_gen)
c627d613
AT
673{
674 int fd1,fd2;
675 struct stat st;
676 char *fname;
677 char fnametmp[MAXPATHLEN];
c6e7fcb4 678 struct map_struct *buf;
c627d613 679 int i;
dc5ddbcc 680 struct file_struct *file;
c6e7fcb4 681 int phase=0;
9e31c482 682 int recv_ok;
c627d613 683
981791bd 684 if (verbose > 2) {
dc5ddbcc 685 fprintf(FERROR,"recv_files(%d) starting\n",flist->count);
981791bd 686 }
c627d613
AT
687
688 if (recurse && delete_mode && !local_name && flist->count>0) {
689 delete_files(flist);
690 }
691
692 while (1)
9e31c482 693 {
c627d613 694 i = read_int(f_in);
c6e7fcb4
AT
695 if (i == -1) {
696 if (phase==0 && remote_version >= 13) {
697 phase++;
9e31c482
AT
698 csum_length = SUM_LENGTH;
699 if (verbose > 2)
700 fprintf(FERROR,"recv_files phase=%d\n",phase);
c6e7fcb4
AT
701 write_int(f_gen,-1);
702 write_flush(f_gen);
703 continue;
704 }
705 break;
706 }
c627d613 707
3ec4dd97
AT
708 file = flist->files[i];
709 fname = f_name(file);
c627d613
AT
710
711 if (local_name)
712 fname = local_name;
713
714 if (dry_run) {
715 if (!am_server && verbose)
716 printf("%s\n",fname);
717 continue;
718 }
719
720 if (verbose > 2)
dc5ddbcc 721 fprintf(FERROR,"recv_files(%s)\n",fname);
c627d613
AT
722
723 /* open the file */
ac1eb754 724 fd1 = open(fname,O_RDONLY);
c627d613 725
ac1eb754 726 if (fd1 != -1 && fstat(fd1,&st) != 0) {
dc5ddbcc 727 fprintf(FERROR,"fstat %s : %s\n",fname,strerror(errno));
7b8356d0 728 receive_data(f_in,NULL,-1,NULL);
c627d613 729 close(fd1);
7b8356d0 730 continue;
c627d613
AT
731 }
732
ac1eb754 733 if (fd1 != -1 && !S_ISREG(st.st_mode)) {
7b8356d0
AT
734 fprintf(FERROR,"%s : not a regular file (recv_files)\n",fname);
735 receive_data(f_in,NULL,-1,NULL);
c627d613 736 close(fd1);
7b8356d0 737 continue;
c627d613
AT
738 }
739
ac1eb754 740 if (fd1 != -1 && st.st_size > 0) {
c627d613 741 buf = map_file(fd1,st.st_size);
9e31c482
AT
742 if (verbose > 2)
743 fprintf(FERROR,"recv mapped %s of size %d\n",fname,(int)st.st_size);
c627d613
AT
744 } else {
745 buf = NULL;
746 }
747
c627d613 748 /* open tmp file */
13a1f792
AT
749 if (strlen(fname) > (MAXPATHLEN-8)) {
750 fprintf(FERROR,"filename too long\n");
3ec4dd97 751 if (buf) unmap_file(buf);
fee64929 752 close(fd1);
13a1f792
AT
753 continue;
754 }
950ab32d
AT
755 if (tmpdir) {
756 char *f;
757 f = strrchr(fname,'/');
758 if (f == NULL)
759 f = fname;
760 else
761 f++;
762 sprintf(fnametmp,"%s/%s.XXXXXX",tmpdir,f);
763 } else {
764 sprintf(fnametmp,"%s.XXXXXX",fname);
765 }
c627d613 766 if (NULL == mktemp(fnametmp)) {
dc5ddbcc 767 fprintf(FERROR,"mktemp %s failed\n",fnametmp);
7b8356d0
AT
768 receive_data(f_in,buf,-1,NULL);
769 if (buf) unmap_file(buf);
770 close(fd1);
771 continue;
c627d613 772 }
31e12522 773 fd2 = do_open(fnametmp,O_WRONLY|O_CREAT|O_EXCL,file->mode);
fee64929 774 if (fd2 == -1 && relative_paths && errno == ENOENT &&
6574b4f7 775 create_directory_path(fnametmp) == 0) {
31e12522 776 fd2 = do_open(fnametmp,O_WRONLY|O_CREAT|O_EXCL,file->mode);
6574b4f7 777 }
c627d613 778 if (fd2 == -1) {
dc5ddbcc 779 fprintf(FERROR,"open %s : %s\n",fnametmp,strerror(errno));
7b8356d0
AT
780 receive_data(f_in,buf,-1,NULL);
781 if (buf) unmap_file(buf);
782 close(fd1);
783 continue;
c627d613
AT
784 }
785
786 cleanup_fname = fnametmp;
787
788 if (!am_server && verbose)
789 printf("%s\n",fname);
790
791 /* recv file data */
9e31c482 792 recv_ok = receive_data(f_in,buf,fd2,fname);
c627d613 793
3ec4dd97 794 if (buf) unmap_file(buf);
981791bd 795 if (fd1 != -1) {
981791bd
AT
796 close(fd1);
797 }
c627d613
AT
798 close(fd2);
799
800 if (verbose > 2)
dc5ddbcc 801 fprintf(FERROR,"renaming %s to %s\n",fnametmp,fname);
c627d613
AT
802
803 if (make_backups) {
804 char fnamebak[MAXPATHLEN];
13a1f792
AT
805 if (strlen(fname) + strlen(backup_suffix) > (MAXPATHLEN-1)) {
806 fprintf(FERROR,"backup filename too long\n");
807 continue;
808 }
c627d613 809 sprintf(fnamebak,"%s%s",fname,backup_suffix);
ac1eb754 810 if (rename(fname,fnamebak) != 0 && errno != ENOENT) {
dc5ddbcc 811 fprintf(FERROR,"rename %s %s : %s\n",fname,fnamebak,strerror(errno));
7b8356d0 812 continue;
c627d613
AT
813 }
814 }
815
816 /* move tmp file over real file */
817 if (rename(fnametmp,fname) != 0) {
950ab32d
AT
818 if (errno == EXDEV) {
819 /* rename failed on cross-filesystem link.
820 Copy the file instead. */
13dc412d 821 if (copy_file(fnametmp,fname, file->mode)) {
950ab32d
AT
822 fprintf(FERROR,"copy %s -> %s : %s\n",
823 fnametmp,fname,strerror(errno));
13dc412d
AT
824 } else {
825 set_perms(fname,file,NULL,0);
826 }
31e12522 827 do_unlink(fnametmp);
950ab32d
AT
828 } else {
829 fprintf(FERROR,"rename %s -> %s : %s\n",
830 fnametmp,fname,strerror(errno));
31e12522 831 do_unlink(fnametmp);
950ab32d 832 }
13dc412d
AT
833 } else {
834 set_perms(fname,file,NULL,0);
c627d613
AT
835 }
836
837 cleanup_fname = NULL;
838
9e31c482
AT
839
840 if (!recv_ok) {
e7ebc36c
AT
841 if (csum_length == SUM_LENGTH) {
842 fprintf(FERROR,"ERROR: file corruption in %s. File changed during transfer?\n",
843 fname);
844 } else {
845 if (verbose > 1)
846 fprintf(FERROR,"redoing %s(%d)\n",fname,i);
847 write_int(f_gen,i);
848 }
9e31c482 849 }
c627d613
AT
850 }
851
7c596906
AT
852 if (preserve_hard_links)
853 do_hard_links(flist);
854
7b8356d0
AT
855 /* now we need to fix any directory permissions that were
856 modified during the transfer */
7c596906 857 for (i = 0; i < flist->count; i++) {
3ec4dd97
AT
858 struct file_struct *file = flist->files[i];
859 if (!file->basename || !S_ISDIR(file->mode)) continue;
860 recv_generator(f_name(file),flist,i,-1);
7b8356d0 861 }
7b8356d0 862
c627d613 863 if (verbose > 2)
dc5ddbcc 864 fprintf(FERROR,"recv_files finished\n");
c627d613
AT
865
866 return 0;
867}
868
869
870
871off_t send_files(struct file_list *flist,int f_out,int f_in)
872{
873 int fd;
874 struct sum_struct *s;
c6e7fcb4 875 struct map_struct *buf;
c627d613
AT
876 struct stat st;
877 char fname[MAXPATHLEN];
878 off_t total=0;
879 int i;
dc5ddbcc 880 struct file_struct *file;
c6e7fcb4 881 int phase = 0;
7796395a 882 int offset=0;
c627d613
AT
883
884 if (verbose > 2)
dc5ddbcc 885 fprintf(FERROR,"send_files starting\n");
c627d613 886
720b47f2
AT
887 setup_nonblocking(f_in,f_out);
888
e7ebc36c
AT
889 while (1) {
890 i = read_int(f_in);
891 if (i == -1) {
892 if (phase==0 && remote_version >= 13) {
893 phase++;
894 csum_length = SUM_LENGTH;
895 write_int(f_out,-1);
896 write_flush(f_out);
897 if (verbose > 2)
898 fprintf(FERROR,"send_files phase=%d\n",phase);
899 continue;
900 }
901 break;
902 }
c627d613 903
e7ebc36c
AT
904 file = flist->files[i];
905
906 fname[0] = 0;
907 if (file->basedir) {
908 strncpy(fname,file->basedir,MAXPATHLEN-1);
909 fname[MAXPATHLEN-1] = 0;
910 if (strlen(fname) == MAXPATHLEN-1) {
911 fprintf(FERROR, "send_files failed on long-named directory %s\n",
912 fname);
913 return -1;
914 }
915 strcat(fname,"/");
7796395a 916 offset = strlen(file->basedir)+1;
e7ebc36c
AT
917 }
918 strncat(fname,f_name(file),MAXPATHLEN-strlen(fname));
919
920 if (verbose > 2)
921 fprintf(FERROR,"send_files(%d,%s)\n",i,fname);
922
923 if (dry_run) {
924 if (!am_server && verbose)
925 printf("%s\n",fname);
926 write_int(f_out,i);
927 continue;
928 }
c627d613 929
e7ebc36c
AT
930 s = receive_sums(f_in);
931 if (!s) {
932 fprintf(FERROR,"receive_sums failed\n");
933 return -1;
934 }
935
936 fd = open(fname,O_RDONLY);
937 if (fd == -1) {
938 fprintf(FERROR,"send_files failed to open %s: %s\n",
939 fname,strerror(errno));
0b910560 940 free_sums(s);
e7ebc36c
AT
941 continue;
942 }
943
944 /* map the local file */
945 if (fstat(fd,&st) != 0) {
946 fprintf(FERROR,"fstat failed : %s\n",strerror(errno));
0b910560 947 free_sums(s);
e7ebc36c
AT
948 close(fd);
949 return -1;
950 }
951
952 if (st.st_size > 0) {
953 buf = map_file(fd,st.st_size);
954 } else {
955 buf = NULL;
956 }
957
958 if (verbose > 2)
959 fprintf(FERROR,"send_files mapped %s of size %d\n",
960 fname,(int)st.st_size);
961
962 write_int(f_out,i);
963
964 write_int(f_out,s->count);
965 write_int(f_out,s->n);
966 write_int(f_out,s->remainder);
967
968 if (verbose > 2)
969 fprintf(FERROR,"calling match_sums %s\n",fname);
970
971 if (!am_server && verbose)
7796395a 972 printf("%s\n",fname+offset);
e7ebc36c
AT
973
974 match_sums(f_out,s,buf,st.st_size);
975 write_flush(f_out);
976
977 if (buf) unmap_file(buf);
978 close(fd);
979
980 free_sums(s);
981
982 if (verbose > 2)
983 fprintf(FERROR,"sender finished %s\n",fname);
984
985 total += st.st_size;
986 }
c627d613 987
dc5ddbcc 988 if (verbose > 2)
e7ebc36c 989 fprintf(FERROR,"send files finished\n");
dc5ddbcc 990
c627d613
AT
991 match_report();
992
993 write_int(f_out,-1);
994 write_flush(f_out);
995
996 return total;
997}
998
999
1000
c6e7fcb4 1001void generate_files(int f,struct file_list *flist,char *local_name,int f_recv)
c627d613
AT
1002{
1003 int i;
9e31c482 1004 int phase=0;
c627d613
AT
1005
1006 if (verbose > 2)
dc5ddbcc 1007 fprintf(FERROR,"generator starting pid=%d count=%d\n",
c627d613
AT
1008 (int)getpid(),flist->count);
1009
1010 for (i = 0; i < flist->count; i++) {
3ec4dd97 1011 struct file_struct *file = flist->files[i];
7b8356d0 1012 mode_t saved_mode = file->mode;
3ec4dd97 1013 if (!file->basename) continue;
7b8356d0
AT
1014
1015 /* we need to ensure that any directories we create have writeable
1016 permissions initially so that we can create the files within
1017 them. This is then fixed after the files are transferred */
1018 if (!am_root && S_ISDIR(file->mode)) {
1019 file->mode |= S_IWUSR; /* user write */
c627d613 1020 }
7b8356d0 1021
3ec4dd97 1022 recv_generator(local_name?local_name:f_name(file),
c627d613 1023 flist,i,f);
7b8356d0
AT
1024
1025 file->mode = saved_mode;
c627d613 1026 }
c6e7fcb4 1027
9e31c482
AT
1028 phase++;
1029 csum_length = SUM_LENGTH;
1030 ignore_times=1;
1031
1032 if (verbose > 2)
1033 fprintf(FERROR,"generate_files phase=%d\n",phase);
1034
c627d613
AT
1035 write_int(f,-1);
1036 write_flush(f);
c6e7fcb4
AT
1037
1038 if (remote_version >= 13) {
7b8356d0
AT
1039 /* in newer versions of the protocol the files can cycle through
1040 the system more than once to catch initial checksum errors */
c6e7fcb4 1041 for (i=read_int(f_recv); i != -1; i=read_int(f_recv)) {
3ec4dd97
AT
1042 struct file_struct *file = flist->files[i];
1043 recv_generator(local_name?local_name:f_name(file),
c6e7fcb4
AT
1044 flist,i,f);
1045 }
1046
9e31c482
AT
1047 phase++;
1048 if (verbose > 2)
1049 fprintf(FERROR,"generate_files phase=%d\n",phase);
1050
c6e7fcb4
AT
1051 write_int(f,-1);
1052 write_flush(f);
1053 }
1054
1055
c627d613 1056 if (verbose > 2)
3a6a366f 1057 fprintf(FERROR,"generator wrote %ld\n",(long)write_total());
c627d613 1058}
dc5ddbcc
AT
1059
1060