| 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 | |
| 22 | extern int verbose; |
| 23 | extern int am_server; |
| 24 | extern int always_checksum; |
| 25 | extern time_t starttime; |
| 26 | |
| 27 | extern char *backup_suffix; |
| 28 | |
| 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; |
| 38 | extern int dry_run; |
| 39 | extern int ignore_times; |
| 40 | extern int recurse; |
| 41 | extern int delete_mode; |
| 42 | extern int cvs_exclude; |
| 43 | |
| 44 | /* |
| 45 | free a sums struct |
| 46 | */ |
| 47 | static void free_sums(struct sum_struct *s) |
| 48 | { |
| 49 | if (s->sums) free(s->sums); |
| 50 | free(s); |
| 51 | } |
| 52 | |
| 53 | |
| 54 | |
| 55 | /* |
| 56 | send a sums struct down a fd |
| 57 | */ |
| 58 | static void send_sums(struct sum_struct *s,int f_out) |
| 59 | { |
| 60 | int i; |
| 61 | |
| 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); |
| 67 | if (s) |
| 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); |
| 71 | } |
| 72 | write_flush(f_out); |
| 73 | } |
| 74 | |
| 75 | |
| 76 | /* |
| 77 | generate a stream of signatures/checksums that describe a buffer |
| 78 | |
| 79 | generate approximately one checksum every n bytes |
| 80 | */ |
| 81 | static struct sum_struct *generate_sums(char *buf,off_t len,int n) |
| 82 | { |
| 83 | int i; |
| 84 | struct sum_struct *s; |
| 85 | int count; |
| 86 | int block_len = n; |
| 87 | int remainder = (len%block_len); |
| 88 | off_t offset = 0; |
| 89 | |
| 90 | count = (len+(block_len-1))/block_len; |
| 91 | |
| 92 | s = (struct sum_struct *)malloc(sizeof(*s)); |
| 93 | if (!s) out_of_memory("generate_sums"); |
| 94 | |
| 95 | s->count = count; |
| 96 | s->remainder = remainder; |
| 97 | s->n = n; |
| 98 | s->flength = len; |
| 99 | |
| 100 | if (count==0) { |
| 101 | s->sums = NULL; |
| 102 | return s; |
| 103 | } |
| 104 | |
| 105 | if (verbose > 3) |
| 106 | fprintf(stderr,"count=%d rem=%d n=%d flength=%d\n", |
| 107 | s->count,s->remainder,s->n,(int)s->flength); |
| 108 | |
| 109 | s->sums = (struct sum_buf *)malloc(sizeof(s->sums[0])*s->count); |
| 110 | if (!s->sums) out_of_memory("generate_sums"); |
| 111 | |
| 112 | for (i=0;i<count;i++) { |
| 113 | int n1 = MIN(len,n); |
| 114 | char *map = map_ptr(buf,offset,n1); |
| 115 | |
| 116 | s->sums[i].sum1 = get_checksum1(map,n1); |
| 117 | get_checksum2(map,n1,s->sums[i].sum2); |
| 118 | |
| 119 | s->sums[i].offset = offset; |
| 120 | s->sums[i].len = n1; |
| 121 | s->sums[i].i = i; |
| 122 | |
| 123 | if (verbose > 3) |
| 124 | fprintf(stderr,"chunk[%d] offset=%d len=%d sum1=%08x\n", |
| 125 | i,(int)s->sums[i].offset,s->sums[i].len,s->sums[i].sum1); |
| 126 | |
| 127 | len -= n1; |
| 128 | offset += n1; |
| 129 | } |
| 130 | |
| 131 | return s; |
| 132 | } |
| 133 | |
| 134 | |
| 135 | /* |
| 136 | receive the checksums for a buffer |
| 137 | */ |
| 138 | static struct sum_struct *receive_sums(int f) |
| 139 | { |
| 140 | struct sum_struct *s; |
| 141 | int i; |
| 142 | off_t offset = 0; |
| 143 | int block_len; |
| 144 | |
| 145 | s = (struct sum_struct *)malloc(sizeof(*s)); |
| 146 | if (!s) out_of_memory("receive_sums"); |
| 147 | |
| 148 | s->count = read_int(f); |
| 149 | s->n = read_int(f); |
| 150 | s->remainder = read_int(f); |
| 151 | s->sums = NULL; |
| 152 | |
| 153 | if (verbose > 3) |
| 154 | fprintf(stderr,"count=%d n=%d rem=%d\n", |
| 155 | s->count,s->n,s->remainder); |
| 156 | |
| 157 | block_len = s->n; |
| 158 | |
| 159 | if (s->count == 0) |
| 160 | return(s); |
| 161 | |
| 162 | s->sums = (struct sum_buf *)malloc(sizeof(s->sums[0])*s->count); |
| 163 | if (!s->sums) out_of_memory("receive_sums"); |
| 164 | |
| 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); |
| 168 | |
| 169 | s->sums[i].offset = offset; |
| 170 | s->sums[i].i = i; |
| 171 | |
| 172 | if (i == s->count-1 && s->remainder != 0) { |
| 173 | s->sums[i].len = s->remainder; |
| 174 | } else { |
| 175 | s->sums[i].len = s->n; |
| 176 | } |
| 177 | offset += s->sums[i].len; |
| 178 | |
| 179 | if (verbose > 3) |
| 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); |
| 182 | } |
| 183 | |
| 184 | s->flength = offset; |
| 185 | |
| 186 | return s; |
| 187 | } |
| 188 | |
| 189 | |
| 190 | static void set_perms(char *fname,struct file_struct *file,struct stat *st, |
| 191 | int report) |
| 192 | { |
| 193 | int updated = 0; |
| 194 | struct stat st2; |
| 195 | |
| 196 | if (dry_run) return; |
| 197 | |
| 198 | if (!st) { |
| 199 | if (stat(fname,&st2) != 0) { |
| 200 | fprintf(stderr,"stat %s : %s\n",fname,strerror(errno)); |
| 201 | return; |
| 202 | } |
| 203 | st = &st2; |
| 204 | } |
| 205 | |
| 206 | if (preserve_times && st->st_mtime != file->modtime) { |
| 207 | updated = 1; |
| 208 | if (set_modtime(fname,file->modtime) != 0) { |
| 209 | fprintf(stderr,"failed to set times on %s : %s\n", |
| 210 | fname,strerror(errno)); |
| 211 | return; |
| 212 | } |
| 213 | } |
| 214 | |
| 215 | #ifdef HAVE_CHMOD |
| 216 | if (preserve_perms && st->st_mode != file->mode) { |
| 217 | updated = 1; |
| 218 | if (chmod(fname,file->mode) != 0) { |
| 219 | fprintf(stderr,"failed to set permissions on %s : %s\n", |
| 220 | fname,strerror(errno)); |
| 221 | return; |
| 222 | } |
| 223 | } |
| 224 | #endif |
| 225 | |
| 226 | if ((preserve_uid && st->st_uid != file->uid) || |
| 227 | (preserve_gid && st->st_gid != file->gid)) { |
| 228 | updated = 1; |
| 229 | if (chown(fname, |
| 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)); |
| 234 | return; |
| 235 | } |
| 236 | } |
| 237 | |
| 238 | if (verbose > 1 && report) { |
| 239 | if (updated) |
| 240 | fprintf(am_server?stderr:stdout,"%s\n",fname); |
| 241 | else |
| 242 | fprintf(am_server?stderr:stdout,"%s is uptodate\n",fname); |
| 243 | } |
| 244 | } |
| 245 | |
| 246 | |
| 247 | void recv_generator(char *fname,struct file_list *flist,int i,int f_out) |
| 248 | { |
| 249 | int fd; |
| 250 | struct stat st; |
| 251 | char *buf; |
| 252 | struct sum_struct *s; |
| 253 | char sum[SUM_LENGTH]; |
| 254 | int statret; |
| 255 | |
| 256 | if (verbose > 2) |
| 257 | fprintf(stderr,"recv_generator(%s)\n",fname); |
| 258 | |
| 259 | statret = lstat(fname,&st); |
| 260 | |
| 261 | #if SUPPORT_LINKS |
| 262 | if (preserve_links && S_ISLNK(flist->files[i].mode)) { |
| 263 | char lnk[MAXPATHLEN]; |
| 264 | int l; |
| 265 | if (statret == 0) { |
| 266 | l = readlink(fname,lnk,MAXPATHLEN-1); |
| 267 | if (l > 0) { |
| 268 | lnk[l] = 0; |
| 269 | if (strcmp(lnk,flist->files[i].link) == 0) { |
| 270 | if (verbose > 1) |
| 271 | fprintf(am_server?stderr:stdout,"%s is uptodate\n",fname); |
| 272 | return; |
| 273 | } |
| 274 | } |
| 275 | } |
| 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)); |
| 280 | } else { |
| 281 | if (verbose) |
| 282 | fprintf(am_server?stderr:stdout,"%s -> %s\n",fname,flist->files[i].link); |
| 283 | } |
| 284 | return; |
| 285 | } |
| 286 | #endif |
| 287 | |
| 288 | #ifdef HAVE_MKNOD |
| 289 | if (preserve_devices && IS_DEVICE(flist->files[i].mode)) { |
| 290 | if (statret != 0 || |
| 291 | st.st_mode != flist->files[i].mode || |
| 292 | st.st_rdev != flist->files[i].dev) { |
| 293 | if (!dry_run) unlink(fname); |
| 294 | if (verbose > 2) |
| 295 | fprintf(stderr,"mknod(%s,0%o,0x%x)\n", |
| 296 | fname,(int)flist->files[i].mode,(int)flist->files[i].dev); |
| 297 | if (!dry_run && |
| 298 | mknod(fname,flist->files[i].mode,flist->files[i].dev) != 0) { |
| 299 | fprintf(stderr,"mknod %s : %s\n",fname,strerror(errno)); |
| 300 | } else { |
| 301 | set_perms(fname,&flist->files[i],NULL,0); |
| 302 | if (verbose) |
| 303 | fprintf(am_server?stderr:stdout,"%s\n",fname); |
| 304 | } |
| 305 | } else { |
| 306 | set_perms(fname,&flist->files[i],&st,1); |
| 307 | } |
| 308 | return; |
| 309 | } |
| 310 | #endif |
| 311 | |
| 312 | if (!S_ISREG(flist->files[i].mode)) { |
| 313 | fprintf(stderr,"skipping non-regular file %s\n",fname); |
| 314 | return; |
| 315 | } |
| 316 | |
| 317 | if (statret == -1) { |
| 318 | if (errno == ENOENT) { |
| 319 | write_int(f_out,i); |
| 320 | if (!dry_run) send_sums(NULL,f_out); |
| 321 | } else { |
| 322 | if (verbose > 1) |
| 323 | fprintf(stderr,"recv_generator failed to open %s\n",fname); |
| 324 | } |
| 325 | return; |
| 326 | } |
| 327 | |
| 328 | if (!S_ISREG(st.st_mode)) { |
| 329 | fprintf(stderr,"%s : not a regular file\n",fname); |
| 330 | return; |
| 331 | } |
| 332 | |
| 333 | if (update_only && st.st_mtime >= flist->files[i].modtime) { |
| 334 | if (verbose > 1) |
| 335 | fprintf(stderr,"%s is newer\n",fname); |
| 336 | return; |
| 337 | } |
| 338 | |
| 339 | if (always_checksum && S_ISREG(st.st_mode)) { |
| 340 | file_checksum(fname,sum,st.st_size); |
| 341 | } |
| 342 | |
| 343 | if (st.st_size == flist->files[i].length && |
| 344 | ((!ignore_times && st.st_mtime == flist->files[i].modtime) || |
| 345 | (always_checksum && S_ISREG(st.st_mode) && |
| 346 | memcmp(sum,flist->files[i].sum,SUM_LENGTH) == 0))) { |
| 347 | set_perms(fname,&flist->files[i],&st,1); |
| 348 | return; |
| 349 | } |
| 350 | |
| 351 | if (dry_run) { |
| 352 | write_int(f_out,i); |
| 353 | return; |
| 354 | } |
| 355 | |
| 356 | /* open the file */ |
| 357 | fd = open(fname,O_RDONLY); |
| 358 | |
| 359 | if (fd == -1) { |
| 360 | fprintf(stderr,"failed to open %s : %s\n",fname,strerror(errno)); |
| 361 | return; |
| 362 | } |
| 363 | |
| 364 | if (st.st_size > 0) { |
| 365 | buf = map_file(fd,st.st_size); |
| 366 | } else { |
| 367 | buf = NULL; |
| 368 | } |
| 369 | |
| 370 | if (verbose > 3) |
| 371 | fprintf(stderr,"mapped %s of size %d\n",fname,(int)st.st_size); |
| 372 | |
| 373 | s = generate_sums(buf,st.st_size,block_size); |
| 374 | |
| 375 | write_int(f_out,i); |
| 376 | send_sums(s,f_out); |
| 377 | write_flush(f_out); |
| 378 | |
| 379 | close(fd); |
| 380 | unmap_file(buf,st.st_size); |
| 381 | |
| 382 | free_sums(s); |
| 383 | } |
| 384 | |
| 385 | |
| 386 | |
| 387 | static void receive_data(int f_in,char *buf,int fd,char *fname) |
| 388 | { |
| 389 | int i,n,remainder,len,count; |
| 390 | off_t offset = 0; |
| 391 | off_t offset2; |
| 392 | |
| 393 | count = read_int(f_in); |
| 394 | n = read_int(f_in); |
| 395 | remainder = read_int(f_in); |
| 396 | |
| 397 | for (i=read_int(f_in); i != 0; i=read_int(f_in)) { |
| 398 | if (i > 0) { |
| 399 | if (verbose > 3) |
| 400 | fprintf(stderr,"data recv %d at %d\n",i,(int)offset); |
| 401 | |
| 402 | if (read_write(f_in,fd,i) != i) { |
| 403 | fprintf(stderr,"write failed on %s : %s\n",fname,strerror(errno)); |
| 404 | exit(1); |
| 405 | } |
| 406 | offset += i; |
| 407 | } else { |
| 408 | i = -(i+1); |
| 409 | offset2 = i*n; |
| 410 | len = n; |
| 411 | if (i == count-1 && remainder != 0) |
| 412 | len = remainder; |
| 413 | |
| 414 | if (verbose > 3) |
| 415 | fprintf(stderr,"chunk[%d] of size %d at %d offset=%d\n", |
| 416 | i,len,(int)offset2,(int)offset); |
| 417 | |
| 418 | if (write(fd,map_ptr(buf,offset2,len),len) != len) { |
| 419 | fprintf(stderr,"write failed on %s : %s\n",fname,strerror(errno)); |
| 420 | exit(1); |
| 421 | } |
| 422 | offset += len; |
| 423 | } |
| 424 | } |
| 425 | } |
| 426 | |
| 427 | |
| 428 | static void delete_one(struct file_struct *f) |
| 429 | { |
| 430 | if (!S_ISDIR(f->mode)) { |
| 431 | if (!dry_run && unlink(f->name) != 0) { |
| 432 | fprintf(stderr,"unlink %s : %s\n",f->name,strerror(errno)); |
| 433 | } else if (verbose) { |
| 434 | fprintf(stderr,"deleting %s\n",f->name); |
| 435 | } |
| 436 | } else { |
| 437 | if (!dry_run && rmdir(f->name) != 0) { |
| 438 | if (errno != ENOTEMPTY) |
| 439 | fprintf(stderr,"rmdir %s : %s\n",f->name,strerror(errno)); |
| 440 | } else if (verbose) { |
| 441 | fprintf(stderr,"deleting directory %s\n",f->name); |
| 442 | } |
| 443 | } |
| 444 | } |
| 445 | |
| 446 | |
| 447 | static void delete_files(struct file_list *flist) |
| 448 | { |
| 449 | struct file_list *local_file_list; |
| 450 | char *dot="."; |
| 451 | int i; |
| 452 | |
| 453 | if (!am_server && cvs_exclude) |
| 454 | add_cvs_excludes(); |
| 455 | if (!(local_file_list = send_file_list(-1,recurse,1,&dot))) |
| 456 | return; |
| 457 | |
| 458 | for (i=local_file_list->count;i>=0;i--) { |
| 459 | if (!local_file_list->files[i].name) continue; |
| 460 | if (-1 == flist_find(flist,&local_file_list->files[i])) { |
| 461 | delete_one(&local_file_list->files[i]); |
| 462 | } |
| 463 | } |
| 464 | } |
| 465 | |
| 466 | static char *cleanup_fname = NULL; |
| 467 | |
| 468 | void sig_int(void) |
| 469 | { |
| 470 | if (cleanup_fname) |
| 471 | unlink(cleanup_fname); |
| 472 | exit(1); |
| 473 | } |
| 474 | |
| 475 | |
| 476 | int recv_files(int f_in,struct file_list *flist,char *local_name) |
| 477 | { |
| 478 | int fd1,fd2; |
| 479 | struct stat st; |
| 480 | char *fname; |
| 481 | char fnametmp[MAXPATHLEN]; |
| 482 | char *buf; |
| 483 | int i; |
| 484 | |
| 485 | if (verbose > 2) { |
| 486 | fprintf(stderr,"recv_files(%d) starting\n",flist->count); |
| 487 | } |
| 488 | |
| 489 | if (recurse && delete_mode && !local_name && flist->count>0) { |
| 490 | delete_files(flist); |
| 491 | } |
| 492 | |
| 493 | while (1) |
| 494 | { |
| 495 | i = read_int(f_in); |
| 496 | if (i == -1) break; |
| 497 | |
| 498 | fname = flist->files[i].name; |
| 499 | |
| 500 | if (local_name) |
| 501 | fname = local_name; |
| 502 | |
| 503 | if (dry_run) { |
| 504 | if (!am_server && verbose) |
| 505 | printf("%s\n",fname); |
| 506 | continue; |
| 507 | } |
| 508 | |
| 509 | if (verbose > 2) |
| 510 | fprintf(stderr,"recv_files(%s)\n",fname); |
| 511 | |
| 512 | /* open the file */ |
| 513 | fd1 = open(fname,O_RDONLY); |
| 514 | |
| 515 | if (fd1 != -1 && fstat(fd1,&st) != 0) { |
| 516 | fprintf(stderr,"fstat %s : %s\n",fname,strerror(errno)); |
| 517 | close(fd1); |
| 518 | return -1; |
| 519 | } |
| 520 | |
| 521 | if (fd1 != -1 && !S_ISREG(st.st_mode)) { |
| 522 | fprintf(stderr,"%s : not a regular file\n",fname); |
| 523 | close(fd1); |
| 524 | return -1; |
| 525 | } |
| 526 | |
| 527 | if (fd1 != -1 && st.st_size > 0) { |
| 528 | buf = map_file(fd1,st.st_size); |
| 529 | } else { |
| 530 | buf = NULL; |
| 531 | } |
| 532 | |
| 533 | if (verbose > 2) |
| 534 | fprintf(stderr,"mapped %s of size %d\n",fname,(int)st.st_size); |
| 535 | |
| 536 | /* open tmp file */ |
| 537 | sprintf(fnametmp,"%s.XXXXXX",fname); |
| 538 | if (NULL == mktemp(fnametmp)) { |
| 539 | fprintf(stderr,"mktemp %s failed\n",fnametmp); |
| 540 | return -1; |
| 541 | } |
| 542 | fd2 = open(fnametmp,O_WRONLY|O_CREAT,flist->files[i].mode); |
| 543 | if (fd2 == -1) { |
| 544 | fprintf(stderr,"open %s : %s\n",fnametmp,strerror(errno)); |
| 545 | return -1; |
| 546 | } |
| 547 | |
| 548 | cleanup_fname = fnametmp; |
| 549 | |
| 550 | if (!am_server && verbose) |
| 551 | printf("%s\n",fname); |
| 552 | |
| 553 | /* recv file data */ |
| 554 | receive_data(f_in,buf,fd2,fname); |
| 555 | |
| 556 | if (fd1 != -1) { |
| 557 | unmap_file(buf,st.st_size); |
| 558 | close(fd1); |
| 559 | } |
| 560 | close(fd2); |
| 561 | |
| 562 | if (verbose > 2) |
| 563 | fprintf(stderr,"renaming %s to %s\n",fnametmp,fname); |
| 564 | |
| 565 | if (make_backups) { |
| 566 | char fnamebak[MAXPATHLEN]; |
| 567 | sprintf(fnamebak,"%s%s",fname,backup_suffix); |
| 568 | if (rename(fname,fnamebak) != 0 && errno != ENOENT) { |
| 569 | fprintf(stderr,"rename %s %s : %s\n",fname,fnamebak,strerror(errno)); |
| 570 | exit(1); |
| 571 | } |
| 572 | } |
| 573 | |
| 574 | /* move tmp file over real file */ |
| 575 | if (rename(fnametmp,fname) != 0) { |
| 576 | fprintf(stderr,"rename %s -> %s : %s\n", |
| 577 | fnametmp,fname,strerror(errno)); |
| 578 | } |
| 579 | |
| 580 | cleanup_fname = NULL; |
| 581 | |
| 582 | set_perms(fname,&flist->files[i],NULL,0); |
| 583 | } |
| 584 | |
| 585 | if (verbose > 2) |
| 586 | fprintf(stderr,"recv_files finished\n"); |
| 587 | |
| 588 | return 0; |
| 589 | } |
| 590 | |
| 591 | |
| 592 | |
| 593 | off_t send_files(struct file_list *flist,int f_out,int f_in) |
| 594 | { |
| 595 | int fd; |
| 596 | struct sum_struct *s; |
| 597 | char *buf; |
| 598 | struct stat st; |
| 599 | char fname[MAXPATHLEN]; |
| 600 | off_t total=0; |
| 601 | int i; |
| 602 | |
| 603 | if (verbose > 2) |
| 604 | fprintf(stderr,"send_files starting\n"); |
| 605 | |
| 606 | setup_nonblocking(f_in,f_out); |
| 607 | |
| 608 | while (1) |
| 609 | { |
| 610 | i = read_int(f_in); |
| 611 | if (i == -1) break; |
| 612 | |
| 613 | fname[0] = 0; |
| 614 | if (flist->files[i].dir) { |
| 615 | strcpy(fname,flist->files[i].dir); |
| 616 | strcat(fname,"/"); |
| 617 | } |
| 618 | strcat(fname,flist->files[i].name); |
| 619 | |
| 620 | if (verbose > 2) |
| 621 | fprintf(stderr,"send_files(%d,%s)\n",i,fname); |
| 622 | |
| 623 | if (dry_run) { |
| 624 | if (!am_server && verbose) |
| 625 | printf("%s\n",fname); |
| 626 | write_int(f_out,i); |
| 627 | continue; |
| 628 | } |
| 629 | |
| 630 | s = receive_sums(f_in); |
| 631 | if (!s) { |
| 632 | fprintf(stderr,"receive_sums failed\n"); |
| 633 | return -1; |
| 634 | } |
| 635 | |
| 636 | fd = open(fname,O_RDONLY); |
| 637 | if (fd == -1) { |
| 638 | fprintf(stderr,"send_files failed to open %s: %s\n", |
| 639 | fname,strerror(errno)); |
| 640 | continue; |
| 641 | } |
| 642 | |
| 643 | /* map the local file */ |
| 644 | if (fstat(fd,&st) != 0) { |
| 645 | fprintf(stderr,"fstat failed : %s\n",strerror(errno)); |
| 646 | return -1; |
| 647 | } |
| 648 | |
| 649 | if (st.st_size > 0) { |
| 650 | buf = map_file(fd,st.st_size); |
| 651 | } else { |
| 652 | buf = NULL; |
| 653 | } |
| 654 | |
| 655 | if (verbose > 2) |
| 656 | fprintf(stderr,"send_files mapped %s of size %d\n", |
| 657 | fname,(int)st.st_size); |
| 658 | |
| 659 | write_int(f_out,i); |
| 660 | |
| 661 | write_int(f_out,s->count); |
| 662 | write_int(f_out,s->n); |
| 663 | write_int(f_out,s->remainder); |
| 664 | |
| 665 | if (verbose > 2) |
| 666 | fprintf(stderr,"calling match_sums %s\n",fname); |
| 667 | |
| 668 | if (!am_server && verbose) |
| 669 | printf("%s\n",fname); |
| 670 | |
| 671 | match_sums(f_out,s,buf,st.st_size); |
| 672 | write_flush(f_out); |
| 673 | |
| 674 | unmap_file(buf,st.st_size); |
| 675 | close(fd); |
| 676 | |
| 677 | free_sums(s); |
| 678 | |
| 679 | if (verbose > 2) |
| 680 | fprintf(stderr,"sender finished %s\n",fname); |
| 681 | |
| 682 | total += st.st_size; |
| 683 | } |
| 684 | |
| 685 | match_report(); |
| 686 | |
| 687 | write_int(f_out,-1); |
| 688 | write_flush(f_out); |
| 689 | |
| 690 | return total; |
| 691 | } |
| 692 | |
| 693 | |
| 694 | |
| 695 | void generate_files(int f,struct file_list *flist,char *local_name) |
| 696 | { |
| 697 | int i; |
| 698 | |
| 699 | if (verbose > 2) |
| 700 | fprintf(stderr,"generator starting pid=%d count=%d\n", |
| 701 | (int)getpid(),flist->count); |
| 702 | |
| 703 | for (i = 0; i < flist->count; i++) { |
| 704 | if (!flist->files[i].name) continue; |
| 705 | if (S_ISDIR(flist->files[i].mode)) { |
| 706 | if (dry_run) continue; |
| 707 | if (mkdir(flist->files[i].name,flist->files[i].mode) != 0 && |
| 708 | errno != EEXIST) { |
| 709 | fprintf(stderr,"mkdir %s : %s\n", |
| 710 | flist->files[i].name,strerror(errno)); |
| 711 | } |
| 712 | continue; |
| 713 | } |
| 714 | recv_generator(local_name?local_name:flist->files[i].name, |
| 715 | flist,i,f); |
| 716 | } |
| 717 | write_int(f,-1); |
| 718 | write_flush(f); |
| 719 | if (verbose > 2) |
| 720 | fprintf(stderr,"generator wrote %d\n",write_total()); |
| 721 | } |