| 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 | time_t starttime = 0; |
| 23 | int64 total_size = 0; |
| 24 | |
| 25 | extern int csum_length; |
| 26 | |
| 27 | extern int verbose; |
| 28 | |
| 29 | static void report(int f) |
| 30 | { |
| 31 | int64 in,out,tsize; |
| 32 | time_t t = time(NULL); |
| 33 | extern int am_server; |
| 34 | extern int am_sender; |
| 35 | extern int am_daemon; |
| 36 | |
| 37 | if (am_daemon) { |
| 38 | syslog(LOG_INFO,"wrote %.0f bytes read %.0f bytes total size %.0f\n", |
| 39 | (double)write_total(),(double)read_total(), |
| 40 | (double)total_size); |
| 41 | if (f == -1 || !am_sender) return; |
| 42 | } |
| 43 | |
| 44 | if (!verbose) return; |
| 45 | |
| 46 | if (am_server && am_sender) { |
| 47 | write_longint(f,read_total()); |
| 48 | write_longint(f,write_total()); |
| 49 | write_longint(f,total_size); |
| 50 | write_flush(f); |
| 51 | return; |
| 52 | } |
| 53 | |
| 54 | if (am_sender) { |
| 55 | in = read_total(); |
| 56 | out = write_total(); |
| 57 | tsize = total_size; |
| 58 | } else { |
| 59 | out = read_longint(f); |
| 60 | in = read_longint(f); |
| 61 | tsize = read_longint(f); |
| 62 | } |
| 63 | |
| 64 | printf("wrote %.0f bytes read %.0f bytes %.2f bytes/sec\n", |
| 65 | (double)out,(double)in,(in+out)/(0.5 + (t-starttime))); |
| 66 | printf("total size is %.0f speedup is %.2f\n", |
| 67 | (double)tsize,(1.0*tsize)/(in+out)); |
| 68 | } |
| 69 | |
| 70 | |
| 71 | static int do_cmd(char *cmd,char *machine,char *user,char *path,int *f_in,int *f_out) |
| 72 | { |
| 73 | char *args[100]; |
| 74 | int i,argc=0, ret; |
| 75 | char *tok,*dir=NULL; |
| 76 | extern int local_server; |
| 77 | extern char *rsync_path; |
| 78 | |
| 79 | if (!local_server) { |
| 80 | if (!cmd) |
| 81 | cmd = getenv(RSYNC_RSH_ENV); |
| 82 | if (!cmd) |
| 83 | cmd = RSYNC_RSH; |
| 84 | cmd = strdup(cmd); |
| 85 | if (!cmd) |
| 86 | goto oom; |
| 87 | |
| 88 | for (tok=strtok(cmd," ");tok;tok=strtok(NULL," ")) { |
| 89 | args[argc++] = tok; |
| 90 | } |
| 91 | |
| 92 | #if HAVE_REMSH |
| 93 | /* remsh (on HPUX) takes the arguments the other way around */ |
| 94 | args[argc++] = machine; |
| 95 | if (user) { |
| 96 | args[argc++] = "-l"; |
| 97 | args[argc++] = user; |
| 98 | } |
| 99 | #else |
| 100 | if (user) { |
| 101 | args[argc++] = "-l"; |
| 102 | args[argc++] = user; |
| 103 | } |
| 104 | args[argc++] = machine; |
| 105 | #endif |
| 106 | |
| 107 | args[argc++] = rsync_path; |
| 108 | |
| 109 | server_options(args,&argc); |
| 110 | } |
| 111 | |
| 112 | args[argc++] = "."; |
| 113 | |
| 114 | if (path && *path) |
| 115 | args[argc++] = path; |
| 116 | |
| 117 | args[argc] = NULL; |
| 118 | |
| 119 | if (verbose > 3) { |
| 120 | rprintf(FINFO,"cmd="); |
| 121 | for (i=0;i<argc;i++) |
| 122 | rprintf(FINFO,"%s ",args[i]); |
| 123 | rprintf(FINFO,"\n"); |
| 124 | } |
| 125 | |
| 126 | if (local_server) { |
| 127 | ret = local_child(argc, args, f_in, f_out); |
| 128 | } else { |
| 129 | ret = piped_child(args,f_in,f_out); |
| 130 | } |
| 131 | |
| 132 | if (dir) free(dir); |
| 133 | |
| 134 | return ret; |
| 135 | |
| 136 | oom: |
| 137 | out_of_memory("do_cmd"); |
| 138 | return 0; /* not reached */ |
| 139 | } |
| 140 | |
| 141 | |
| 142 | |
| 143 | |
| 144 | static char *get_local_name(struct file_list *flist,char *name) |
| 145 | { |
| 146 | STRUCT_STAT st; |
| 147 | extern int orig_umask; |
| 148 | |
| 149 | if (do_stat(name,&st) == 0) { |
| 150 | if (S_ISDIR(st.st_mode)) { |
| 151 | if (chdir(name) != 0) { |
| 152 | rprintf(FERROR,"chdir %s : %s (1)\n",name,strerror(errno)); |
| 153 | exit_cleanup(1); |
| 154 | } |
| 155 | return NULL; |
| 156 | } |
| 157 | if (flist->count > 1) { |
| 158 | rprintf(FERROR,"ERROR: destination must be a directory when copying more than 1 file\n"); |
| 159 | exit_cleanup(1); |
| 160 | } |
| 161 | return name; |
| 162 | } |
| 163 | |
| 164 | if (flist->count == 1) |
| 165 | return name; |
| 166 | |
| 167 | if (!name) |
| 168 | return NULL; |
| 169 | |
| 170 | if (do_mkdir(name,0777 & ~orig_umask) != 0) { |
| 171 | rprintf(FERROR,"mkdir %s : %s (1)\n",name,strerror(errno)); |
| 172 | exit_cleanup(1); |
| 173 | } else { |
| 174 | rprintf(FINFO,"created directory %s\n",name); |
| 175 | } |
| 176 | |
| 177 | if (chdir(name) != 0) { |
| 178 | rprintf(FERROR,"chdir %s : %s (2)\n",name,strerror(errno)); |
| 179 | exit_cleanup(1); |
| 180 | } |
| 181 | |
| 182 | return NULL; |
| 183 | } |
| 184 | |
| 185 | |
| 186 | |
| 187 | |
| 188 | static void do_server_sender(int f_in, int f_out, int argc,char *argv[]) |
| 189 | { |
| 190 | int i; |
| 191 | struct file_list *flist; |
| 192 | char *dir = argv[0]; |
| 193 | extern int relative_paths; |
| 194 | extern int recurse; |
| 195 | |
| 196 | if (verbose > 2) |
| 197 | rprintf(FINFO,"server_sender starting pid=%d\n",(int)getpid()); |
| 198 | |
| 199 | if (!relative_paths && chdir(dir) != 0) { |
| 200 | rprintf(FERROR,"chdir %s: %s (3)\n",dir,strerror(errno)); |
| 201 | exit_cleanup(1); |
| 202 | } |
| 203 | argc--; |
| 204 | argv++; |
| 205 | |
| 206 | if (strcmp(dir,".")) { |
| 207 | int l = strlen(dir); |
| 208 | if (strcmp(dir,"/") == 0) |
| 209 | l = 0; |
| 210 | for (i=0;i<argc;i++) |
| 211 | argv[i] += l+1; |
| 212 | } |
| 213 | |
| 214 | if (argc == 0 && recurse) { |
| 215 | argc=1; |
| 216 | argv--; |
| 217 | argv[0] = "."; |
| 218 | } |
| 219 | |
| 220 | flist = send_file_list(f_out,argc,argv); |
| 221 | if (!flist || flist->count == 0) { |
| 222 | exit_cleanup(0); |
| 223 | } |
| 224 | |
| 225 | send_files(flist,f_out,f_in); |
| 226 | report(f_out); |
| 227 | io_flush(); |
| 228 | exit_cleanup(0); |
| 229 | } |
| 230 | |
| 231 | |
| 232 | static int do_recv(int f_in,int f_out,struct file_list *flist,char *local_name) |
| 233 | { |
| 234 | int pid; |
| 235 | int status=0; |
| 236 | int recv_pipe[2]; |
| 237 | extern int preserve_hard_links; |
| 238 | extern int am_server; |
| 239 | |
| 240 | if (preserve_hard_links) |
| 241 | init_hard_links(flist); |
| 242 | |
| 243 | if (pipe(recv_pipe) < 0) { |
| 244 | rprintf(FERROR,"pipe failed in do_recv\n"); |
| 245 | exit_cleanup(1); |
| 246 | } |
| 247 | |
| 248 | io_flush(); |
| 249 | |
| 250 | if ((pid=do_fork()) == 0) { |
| 251 | close(recv_pipe[1]); |
| 252 | io_close_input(f_in); |
| 253 | if (f_in != f_out) close(f_in); |
| 254 | generate_files(f_out,flist,local_name,recv_pipe[0]); |
| 255 | |
| 256 | io_flush(); |
| 257 | _exit(0); |
| 258 | } |
| 259 | |
| 260 | |
| 261 | close(recv_pipe[0]); |
| 262 | if (f_in != f_out) close(f_out); |
| 263 | |
| 264 | recv_files(f_in,flist,local_name,recv_pipe[1]); |
| 265 | if (!am_server) |
| 266 | report(f_in); |
| 267 | |
| 268 | if (verbose > 3) |
| 269 | rprintf(FINFO,"do_recv waiting on %d\n",pid); |
| 270 | |
| 271 | io_flush(); |
| 272 | waitpid(pid, &status, 0); |
| 273 | |
| 274 | return status; |
| 275 | } |
| 276 | |
| 277 | |
| 278 | static void do_server_recv(int f_in, int f_out, int argc,char *argv[]) |
| 279 | { |
| 280 | int status; |
| 281 | struct file_list *flist; |
| 282 | char *local_name=NULL; |
| 283 | char *dir = NULL; |
| 284 | extern int delete_mode; |
| 285 | extern int am_daemon; |
| 286 | |
| 287 | if (verbose > 2) |
| 288 | rprintf(FINFO,"server_recv(%d) starting pid=%d\n",argc,(int)getpid()); |
| 289 | |
| 290 | if (argc > 0) { |
| 291 | dir = argv[0]; |
| 292 | argc--; |
| 293 | argv++; |
| 294 | if (!am_daemon && chdir(dir) != 0) { |
| 295 | rprintf(FERROR,"chdir %s : %s (4)\n", |
| 296 | dir,strerror(errno)); |
| 297 | exit_cleanup(1); |
| 298 | } |
| 299 | } |
| 300 | |
| 301 | if (delete_mode) |
| 302 | recv_exclude_list(f_in); |
| 303 | |
| 304 | flist = recv_file_list(f_in); |
| 305 | if (!flist || flist->count == 0) { |
| 306 | rprintf(FERROR,"server_recv: nothing to do\n"); |
| 307 | exit_cleanup(1); |
| 308 | } |
| 309 | |
| 310 | if (argc > 0) { |
| 311 | if (strcmp(dir,".")) { |
| 312 | argv[0] += strlen(dir); |
| 313 | if (argv[0][0] == '/') argv[0]++; |
| 314 | } |
| 315 | local_name = get_local_name(flist,argv[0]); |
| 316 | } |
| 317 | |
| 318 | status = do_recv(f_in,f_out,flist,local_name); |
| 319 | exit_cleanup(status); |
| 320 | } |
| 321 | |
| 322 | |
| 323 | void start_server(int f_in, int f_out, int argc, char *argv[]) |
| 324 | { |
| 325 | extern int cvs_exclude; |
| 326 | extern int am_sender; |
| 327 | |
| 328 | setup_protocol(f_out, f_in); |
| 329 | |
| 330 | if (am_sender) { |
| 331 | recv_exclude_list(f_in); |
| 332 | if (cvs_exclude) |
| 333 | add_cvs_excludes(); |
| 334 | do_server_sender(f_in, f_out, argc, argv); |
| 335 | } else { |
| 336 | do_server_recv(f_in, f_out, argc, argv); |
| 337 | } |
| 338 | exit_cleanup(0); |
| 339 | } |
| 340 | |
| 341 | int client_run(int f_in, int f_out, int pid, int argc, char *argv[]) |
| 342 | { |
| 343 | struct file_list *flist; |
| 344 | int status = 0, status2 = 0; |
| 345 | char *local_name = NULL; |
| 346 | extern int am_sender; |
| 347 | |
| 348 | setup_protocol(f_out,f_in); |
| 349 | |
| 350 | if (am_sender) { |
| 351 | extern int cvs_exclude; |
| 352 | extern int delete_mode; |
| 353 | if (cvs_exclude) |
| 354 | add_cvs_excludes(); |
| 355 | if (delete_mode) |
| 356 | send_exclude_list(f_out); |
| 357 | flist = send_file_list(f_out,argc,argv); |
| 358 | if (verbose > 3) |
| 359 | rprintf(FINFO,"file list sent\n"); |
| 360 | send_files(flist,f_out,f_in); |
| 361 | if (pid != -1) { |
| 362 | if (verbose > 3) |
| 363 | rprintf(FINFO,"client_run waiting on %d\n",pid); |
| 364 | io_flush(); |
| 365 | waitpid(pid, &status, 0); |
| 366 | } |
| 367 | report(-1); |
| 368 | exit_cleanup(status); |
| 369 | } |
| 370 | |
| 371 | send_exclude_list(f_out); |
| 372 | |
| 373 | flist = recv_file_list(f_in); |
| 374 | if (!flist || flist->count == 0) { |
| 375 | rprintf(FINFO,"client: nothing to do\n"); |
| 376 | exit_cleanup(0); |
| 377 | } |
| 378 | |
| 379 | local_name = get_local_name(flist,argv[0]); |
| 380 | |
| 381 | status2 = do_recv(f_in,f_out,flist,local_name); |
| 382 | |
| 383 | if (pid != -1) { |
| 384 | if (verbose > 3) |
| 385 | rprintf(FINFO,"client_run2 waiting on %d\n",pid); |
| 386 | io_flush(); |
| 387 | waitpid(pid, &status, 0); |
| 388 | } |
| 389 | |
| 390 | return status | status2; |
| 391 | } |
| 392 | |
| 393 | |
| 394 | int start_client(int argc, char *argv[]) |
| 395 | { |
| 396 | char *p; |
| 397 | char *shell_machine = NULL; |
| 398 | char *shell_path = NULL; |
| 399 | char *shell_user = NULL; |
| 400 | int pid; |
| 401 | int f_in,f_out; |
| 402 | extern int local_server; |
| 403 | extern int am_sender; |
| 404 | extern char *shell_cmd; |
| 405 | |
| 406 | p = strchr(argv[0],':'); |
| 407 | |
| 408 | if (p) { |
| 409 | if (p[1] == ':') { |
| 410 | *p = 0; |
| 411 | return start_socket_client(argv[0], p+2, argc-1, argv+1); |
| 412 | } |
| 413 | |
| 414 | if (argc < 2) { |
| 415 | usage(FERROR); |
| 416 | exit_cleanup(1); |
| 417 | } |
| 418 | |
| 419 | am_sender = 0; |
| 420 | *p = 0; |
| 421 | shell_machine = argv[0]; |
| 422 | shell_path = p+1; |
| 423 | argc--; |
| 424 | argv++; |
| 425 | } else { |
| 426 | am_sender = 1; |
| 427 | |
| 428 | p = strchr(argv[argc-1],':'); |
| 429 | if (!p) { |
| 430 | local_server = 1; |
| 431 | } else if (p[1] == ':') { |
| 432 | *p = 0; |
| 433 | return start_socket_client(argv[argc-1], p+2, argc-1, argv); |
| 434 | } |
| 435 | |
| 436 | if (argc < 2) { |
| 437 | usage(FERROR); |
| 438 | exit_cleanup(1); |
| 439 | } |
| 440 | |
| 441 | if (local_server) { |
| 442 | shell_machine = NULL; |
| 443 | shell_path = argv[argc-1]; |
| 444 | } else { |
| 445 | *p = 0; |
| 446 | shell_machine = argv[argc-1]; |
| 447 | shell_path = p+1; |
| 448 | } |
| 449 | argc--; |
| 450 | } |
| 451 | |
| 452 | if (shell_machine) { |
| 453 | p = strchr(shell_machine,'@'); |
| 454 | if (p) { |
| 455 | *p = 0; |
| 456 | shell_user = shell_machine; |
| 457 | shell_machine = p+1; |
| 458 | } |
| 459 | } |
| 460 | |
| 461 | if (verbose > 3) { |
| 462 | rprintf(FINFO,"cmd=%s machine=%s user=%s path=%s\n", |
| 463 | shell_cmd?shell_cmd:"", |
| 464 | shell_machine?shell_machine:"", |
| 465 | shell_user?shell_user:"", |
| 466 | shell_path?shell_path:""); |
| 467 | } |
| 468 | |
| 469 | if (!am_sender && argc != 1) { |
| 470 | usage(FERROR); |
| 471 | exit_cleanup(1); |
| 472 | } |
| 473 | |
| 474 | pid = do_cmd(shell_cmd,shell_machine,shell_user,shell_path,&f_in,&f_out); |
| 475 | |
| 476 | #if HAVE_SETLINEBUF |
| 477 | setlinebuf(stdout); |
| 478 | setlinebuf(stderr); |
| 479 | #endif |
| 480 | |
| 481 | return client_run(f_in, f_out, pid, argc, argv); |
| 482 | } |
| 483 | |
| 484 | |
| 485 | RETSIGTYPE sigusr1_handler(int val) { |
| 486 | exit_cleanup(1); |
| 487 | } |
| 488 | |
| 489 | int main(int argc,char *argv[]) |
| 490 | { |
| 491 | extern int am_root; |
| 492 | extern int orig_umask; |
| 493 | extern int dry_run; |
| 494 | extern int am_daemon; |
| 495 | extern int am_server; |
| 496 | |
| 497 | signal(SIGUSR1, sigusr1_handler); |
| 498 | |
| 499 | starttime = time(NULL); |
| 500 | am_root = (getuid() == 0); |
| 501 | |
| 502 | if (argc < 2) { |
| 503 | usage(FERROR); |
| 504 | exit_cleanup(1); |
| 505 | } |
| 506 | |
| 507 | /* we set a 0 umask so that correct file permissions can be |
| 508 | carried across */ |
| 509 | orig_umask = (int)umask(0); |
| 510 | |
| 511 | parse_arguments(argc, argv); |
| 512 | |
| 513 | argc -= optind; |
| 514 | argv += optind; |
| 515 | optind = 0; |
| 516 | |
| 517 | signal(SIGCHLD,SIG_IGN); |
| 518 | signal(SIGINT,SIGNAL_CAST sig_int); |
| 519 | signal(SIGPIPE,SIGNAL_CAST sig_int); |
| 520 | signal(SIGHUP,SIGNAL_CAST sig_int); |
| 521 | |
| 522 | if (am_daemon) { |
| 523 | return daemon_main(); |
| 524 | } |
| 525 | |
| 526 | if (argc < 1) { |
| 527 | usage(FERROR); |
| 528 | exit_cleanup(1); |
| 529 | } |
| 530 | |
| 531 | if (dry_run) |
| 532 | verbose = MAX(verbose,1); |
| 533 | |
| 534 | #ifndef SUPPORT_LINKS |
| 535 | if (!am_server && preserve_links) { |
| 536 | rprintf(FERROR,"ERROR: symbolic links not supported\n"); |
| 537 | exit_cleanup(1); |
| 538 | } |
| 539 | #endif |
| 540 | |
| 541 | if (am_server) { |
| 542 | start_server(STDIN_FILENO, STDOUT_FILENO, argc, argv); |
| 543 | } |
| 544 | |
| 545 | return start_client(argc, argv); |
| 546 | } |
| 547 | |