better negotiation of protocol versions
[rsync/rsync.git] / main.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
22int verbose = 0;
23int always_checksum = 0;
f0fca04e 24time_t starttime = 0;
71c46176 25int64 total_size = 0;
c627d613
AT
26int block_size=BLOCK_SIZE;
27
28char *backup_suffix = BACKUP_SUFFIX;
950ab32d 29char *tmpdir = NULL;
c627d613
AT
30
31static char *rsync_path = RSYNC_NAME;
32
33int make_backups = 0;
82306bf6
AT
34int whole_file = 0;
35int copy_links = 0;
c627d613 36int preserve_links = 0;
dc5ddbcc 37int preserve_hard_links = 0;
c627d613
AT
38int preserve_perms = 0;
39int preserve_devices = 0;
40int preserve_uid = 0;
41int preserve_gid = 0;
42int preserve_times = 0;
43int update_only = 0;
44int cvs_exclude = 0;
45int dry_run=0;
46int local_server=0;
47int ignore_times=0;
48int delete_mode=0;
49int one_file_system=0;
4fe159a8 50int remote_version=0;
dc5ddbcc 51int sparse_files=0;
70d794dc 52int do_compression=0;
7b8356d0
AT
53int am_root=0;
54int orig_umask=0;
6574b4f7 55int relative_paths=0;
f6c34742 56int numeric_ids = 0;
3cb6f5d6 57int force_delete = 0;
6ba9279f
AT
58int io_timeout = 0;
59int io_error = 0;
f0fca04e 60int read_only = 0;
3591c066 61int module_id = -1;
f0fca04e 62
3591c066 63int rsync_port = RSYNC_PORT;
70d794dc 64
5d6bcd44
AT
65static char *shell_cmd;
66
34ccb63e 67extern int csum_length;
c627d613
AT
68
69int am_server = 0;
f0fca04e 70int am_sender=0;
c627d613 71int recurse = 0;
f0fca04e 72int am_daemon=0;
c627d613 73
9486289c 74static void usage(int fd);
c627d613
AT
75
76static void report(int f)
77{
71c46176 78 int64 in,out,tsize;
c627d613
AT
79 time_t t = time(NULL);
80
81 if (!verbose) return;
82
366345fe 83 if (am_server && am_sender) {
3a6a366f
AT
84 write_longint(f,read_total());
85 write_longint(f,write_total());
86 write_longint(f,total_size);
c627d613
AT
87 write_flush(f);
88 return;
89 }
90
366345fe 91 if (am_sender) {
c627d613
AT
92 in = read_total();
93 out = write_total();
3a6a366f 94 tsize = total_size;
c627d613 95 } else {
3a6a366f
AT
96 in = read_longint(f);
97 out = read_longint(f);
98 tsize = read_longint(f);
c627d613
AT
99 }
100
efb2f6bf
AT
101 printf("wrote %.0f bytes read %.0f bytes %.2f bytes/sec\n",
102 (double)out,(double)in,(in+out)/(0.5 + (t-starttime)));
103 printf("total size is %.0f speedup is %.2f\n",
104 (double)tsize,(1.0*tsize)/(in+out));
c627d613
AT
105}
106
107
3591c066 108void server_options(char **args,int *argc)
c627d613
AT
109{
110 int ac = *argc;
111 static char argstr[50];
112 static char bsize[30];
6ba9279f 113 static char iotime[30];
c627d613
AT
114 int i, x;
115
116 args[ac++] = "--server";
117
366345fe 118 if (!am_sender)
c627d613
AT
119 args[ac++] = "--sender";
120
121 x = 1;
122 argstr[0] = '-';
123 for (i=0;i<verbose;i++)
124 argstr[x++] = 'v';
125 if (make_backups)
126 argstr[x++] = 'b';
127 if (update_only)
128 argstr[x++] = 'u';
129 if (dry_run)
130 argstr[x++] = 'n';
131 if (preserve_links)
132 argstr[x++] = 'l';
82306bf6
AT
133 if (copy_links)
134 argstr[x++] = 'L';
135 if (whole_file)
136 argstr[x++] = 'W';
dc5ddbcc
AT
137 if (preserve_hard_links)
138 argstr[x++] = 'H';
c627d613
AT
139 if (preserve_uid)
140 argstr[x++] = 'o';
141 if (preserve_gid)
142 argstr[x++] = 'g';
143 if (preserve_devices)
144 argstr[x++] = 'D';
145 if (preserve_times)
146 argstr[x++] = 't';
147 if (preserve_perms)
148 argstr[x++] = 'p';
149 if (recurse)
150 argstr[x++] = 'r';
151 if (always_checksum)
152 argstr[x++] = 'c';
153 if (cvs_exclude)
154 argstr[x++] = 'C';
155 if (ignore_times)
156 argstr[x++] = 'I';
6574b4f7
AT
157 if (relative_paths)
158 argstr[x++] = 'R';
c627d613
AT
159 if (one_file_system)
160 argstr[x++] = 'x';
dc5ddbcc
AT
161 if (sparse_files)
162 argstr[x++] = 'S';
861c20b4
PM
163 if (do_compression)
164 argstr[x++] = 'z';
c627d613
AT
165 argstr[x] = 0;
166
167 if (x != 1) args[ac++] = argstr;
168
169 if (block_size != BLOCK_SIZE) {
170 sprintf(bsize,"-B%d",block_size);
171 args[ac++] = bsize;
172 }
43a481dc 173
6ba9279f
AT
174 if (io_timeout) {
175 sprintf(iotime,"--timeout=%d",io_timeout);
176 args[ac++] = iotime;
177 }
178
dc7a9478
AT
179 if (strcmp(backup_suffix, BACKUP_SUFFIX)) {
180 args[ac++] = "--suffix";
181 args[ac++] = backup_suffix;
182 }
183
c627d613
AT
184 if (delete_mode)
185 args[ac++] = "--delete";
186
3cb6f5d6
AT
187 if (force_delete)
188 args[ac++] = "--force";
189
f6c34742
AT
190 if (numeric_ids)
191 args[ac++] = "--numeric-ids";
192
950ab32d
AT
193 if (tmpdir) {
194 args[ac++] = "--temp-dir";
195 args[ac++] = tmpdir;
196 }
197
c627d613
AT
198 *argc = ac;
199}
200
201
202
e3cd198f 203static int do_cmd(char *cmd,char *machine,char *user,char *path,int *f_in,int *f_out)
c627d613 204{
366345fe
AT
205 char *args[100];
206 int i,argc=0, ret;
207 char *tok,*dir=NULL;
208
209 if (!local_server) {
210 if (!cmd)
211 cmd = getenv(RSYNC_RSH_ENV);
212 if (!cmd)
213 cmd = RSYNC_RSH;
214 cmd = strdup(cmd);
215 if (!cmd)
216 goto oom;
217
218 for (tok=strtok(cmd," ");tok;tok=strtok(NULL," ")) {
219 args[argc++] = tok;
220 }
c627d613 221
7b8356d0 222#if HAVE_REMSH
366345fe
AT
223 /* remsh (on HPUX) takes the arguments the other way around */
224 args[argc++] = machine;
225 if (user) {
226 args[argc++] = "-l";
227 args[argc++] = user;
228 }
7b8356d0 229#else
366345fe
AT
230 if (user) {
231 args[argc++] = "-l";
232 args[argc++] = user;
233 }
234 args[argc++] = machine;
7b8356d0 235#endif
c627d613 236
366345fe 237 args[argc++] = rsync_path;
c627d613 238
366345fe
AT
239 server_options(args,&argc);
240 }
c627d613 241
366345fe 242 args[argc++] = ".";
76076c4b 243
366345fe
AT
244 if (path && *path)
245 args[argc++] = path;
c627d613 246
366345fe 247 args[argc] = NULL;
c627d613 248
366345fe 249 if (verbose > 3) {
9486289c 250 rprintf(FINFO,"cmd=");
366345fe 251 for (i=0;i<argc;i++)
9486289c
AT
252 rprintf(FINFO,"%s ",args[i]);
253 rprintf(FINFO,"\n");
366345fe
AT
254 }
255
256 if (local_server) {
257 ret = local_child(argc, args, f_in, f_out);
258 } else {
259 ret = piped_child(args,f_in,f_out);
260 }
c627d613 261
366345fe 262 if (dir) free(dir);
82306bf6 263
366345fe 264 return ret;
c627d613
AT
265
266oom:
366345fe
AT
267 out_of_memory("do_cmd");
268 return 0; /* not reached */
c627d613
AT
269}
270
271
272
273
274static char *get_local_name(struct file_list *flist,char *name)
275{
bcacc18b 276 STRUCT_STAT st;
c627d613 277
bcacc18b 278 if (do_stat(name,&st) == 0) {
c627d613
AT
279 if (S_ISDIR(st.st_mode)) {
280 if (chdir(name) != 0) {
9486289c 281 rprintf(FERROR,"chdir %s : %s (1)\n",name,strerror(errno));
34ccb63e 282 exit_cleanup(1);
c627d613
AT
283 }
284 return NULL;
285 }
286 if (flist->count > 1) {
9486289c 287 rprintf(FERROR,"ERROR: destination must be a directory when copying more than 1 file\n");
34ccb63e 288 exit_cleanup(1);
c627d613
AT
289 }
290 return name;
291 }
292
293 if (flist->count == 1)
294 return name;
295
296 if (!name)
297 return NULL;
298
1b2d733a 299 if (do_mkdir(name,0777 & ~orig_umask) != 0) {
9486289c 300 rprintf(FERROR,"mkdir %s : %s (1)\n",name,strerror(errno));
34ccb63e 301 exit_cleanup(1);
94481d91 302 } else {
9486289c 303 rprintf(FINFO,"created directory %s\n",name);
c627d613
AT
304 }
305
306 if (chdir(name) != 0) {
9486289c 307 rprintf(FERROR,"chdir %s : %s (2)\n",name,strerror(errno));
34ccb63e 308 exit_cleanup(1);
c627d613
AT
309 }
310
311 return NULL;
312}
313
314
315
316
9486289c 317static void do_server_sender(int f_in, int f_out, int argc,char *argv[])
c627d613
AT
318{
319 int i;
c627d613 320 struct file_list *flist;
76076c4b 321 char *dir = argv[0];
c627d613
AT
322
323 if (verbose > 2)
9486289c 324 rprintf(FINFO,"server_sender starting pid=%d\n",(int)getpid());
c627d613 325
6574b4f7 326 if (!relative_paths && chdir(dir) != 0) {
9486289c 327 rprintf(FERROR,"chdir %s: %s (3)\n",dir,strerror(errno));
76076c4b 328 exit_cleanup(1);
c627d613
AT
329 }
330 argc--;
331 argv++;
332
333 if (strcmp(dir,".")) {
76076c4b
AT
334 int l = strlen(dir);
335 if (strcmp(dir,"/") == 0)
336 l = 0;
337 for (i=0;i<argc;i++)
338 argv[i] += l+1;
c627d613
AT
339 }
340
f0fca04e
AT
341 if (am_daemon) {
342 char *name = lp_name(module_id);
343 int l = strlen(name);
344 for (i=0;i<argc;i++) {
345 if (strncmp(argv[i], name, l) == 0) {
346 argv[i] += l;
347 if (!*argv[i]) argv[i] = ".";
348 }
349 }
350 }
351
c627d613 352 if (argc == 0 && recurse) {
76076c4b
AT
353 argc=1;
354 argv--;
355 argv[0] = ".";
c627d613 356 }
f0fca04e
AT
357
358 rprintf(FINFO,"sending file list\n");
c627d613 359
9486289c
AT
360 flist = send_file_list(f_out,argc,argv);
361 send_files(flist,f_out,f_in);
362 report(f_out);
34ccb63e 363 exit_cleanup(0);
c627d613
AT
364}
365
366
dc5ddbcc
AT
367static int do_recv(int f_in,int f_out,struct file_list *flist,char *local_name)
368{
369 int pid;
370 int status=0;
c6e7fcb4 371 int recv_pipe[2];
dc5ddbcc
AT
372
373 if (preserve_hard_links)
374 init_hard_links(flist);
375
c6e7fcb4 376 if (pipe(recv_pipe) < 0) {
9486289c 377 rprintf(FERROR,"pipe failed in do_recv\n");
c6e7fcb4
AT
378 exit(1);
379 }
380
381
3ba62a83 382 if ((pid=do_fork()) == 0) {
c6e7fcb4 383 recv_files(f_in,flist,local_name,recv_pipe[1]);
dc5ddbcc 384 if (verbose > 2)
9486289c 385 rprintf(FINFO,"receiver read %ld\n",(long)read_total());
dc5ddbcc
AT
386 exit_cleanup(0);
387 }
388
c6e7fcb4 389 generate_files(f_out,flist,local_name,recv_pipe[0]);
dc5ddbcc
AT
390
391 waitpid(pid, &status, 0);
392
393 return status;
394}
395
c627d613 396
9486289c 397static void do_server_recv(int f_in, int f_out, int argc,char *argv[])
c627d613 398{
dc5ddbcc 399 int status;
c627d613
AT
400 struct file_list *flist;
401 char *local_name=NULL;
76076c4b 402 char *dir = NULL;
c627d613
AT
403
404 if (verbose > 2)
9486289c 405 rprintf(FINFO,"server_recv(%d) starting pid=%d\n",argc,(int)getpid());
c627d613 406
f0fca04e
AT
407 if (am_daemon) {
408 char *name = lp_name(module_id);
409 int i, l = strlen(name);
410 for (i=0;i<argc;i++) {
411 if (strncmp(argv[i], name, l) == 0) {
412 argv[i] += l;
413 if (!*argv[i]) argv[i] = ".";
414 }
415 rprintf(FINFO,"argv[%d]=%s\n", i, argv[i]);
416 }
417 }
418
c627d613 419 if (argc > 0) {
76076c4b
AT
420 dir = argv[0];
421 argc--;
422 argv++;
f0fca04e 423 if (!am_daemon && chdir(dir) != 0) {
9486289c 424 rprintf(FERROR,"chdir %s : %s (4)\n",
76076c4b
AT
425 dir,strerror(errno));
426 exit_cleanup(1);
427 }
c627d613
AT
428 }
429
430 if (delete_mode)
9486289c 431 recv_exclude_list(f_in);
c627d613 432
9486289c 433 flist = recv_file_list(f_in);
c627d613 434 if (!flist || flist->count == 0) {
9486289c 435 rprintf(FERROR,"nothing to do\n");
34ccb63e 436 exit_cleanup(1);
c627d613
AT
437 }
438
439 if (argc > 0) {
76076c4b
AT
440 if (strcmp(dir,".")) {
441 argv[0] += strlen(dir);
442 if (argv[0][0] == '/') argv[0]++;
443 }
444 local_name = get_local_name(flist,argv[0]);
c627d613
AT
445 }
446
9486289c 447 status = do_recv(f_in,f_out,flist,local_name);
34ccb63e 448 exit_cleanup(status);
c627d613
AT
449}
450
451
9486289c 452void start_server(int f_in, int f_out, int argc, char *argv[])
366345fe 453{
9486289c 454 setup_protocol(f_out, f_in);
366345fe
AT
455
456 if (am_sender) {
9486289c 457 recv_exclude_list(f_in);
366345fe
AT
458 if (cvs_exclude)
459 add_cvs_excludes();
9486289c 460 do_server_sender(f_in, f_out, argc, argv);
366345fe 461 } else {
9486289c 462 do_server_recv(f_in, f_out, argc, argv);
366345fe
AT
463 }
464 exit_cleanup(0);
465}
466
3591c066 467int client_run(int f_in, int f_out, int pid, int argc, char *argv[])
9486289c
AT
468{
469 struct file_list *flist;
470 int status = 0, status2 = 0;
471 char *local_name = NULL;
472
473 setup_protocol(f_out,f_in);
474
475 if (am_sender) {
476 if (cvs_exclude)
477 add_cvs_excludes();
478 if (delete_mode)
479 send_exclude_list(f_out);
480 flist = send_file_list(f_out,argc,argv);
481 if (verbose > 3)
482 rprintf(FINFO,"file list sent\n");
483 send_files(flist,f_out,f_in);
484 if (pid != -1) {
485 if (verbose > 3)
486 rprintf(FINFO,"waiting on %d\n",pid);
487 waitpid(pid, &status, 0);
488 }
489 report(-1);
490 exit_cleanup(status);
491 }
492
493 send_exclude_list(f_out);
494
495 flist = recv_file_list(f_in);
496 if (!flist || flist->count == 0) {
497 rprintf(FINFO,"nothing to do\n");
498 exit_cleanup(0);
499 }
500
501 local_name = get_local_name(flist,argv[0]);
502
503 status2 = do_recv(f_in,f_out,flist,local_name);
504
505 report(f_in);
506
507 if (pid != -1) {
508 waitpid(pid, &status, 0);
509 }
510
511 return status | status2;
512}
513
514
5d6bcd44
AT
515int start_client(int argc, char *argv[])
516{
517 char *p;
518 char *shell_machine = NULL;
519 char *shell_path = NULL;
520 char *shell_user = NULL;
9486289c 521 int pid;
5d6bcd44 522 int f_in,f_out;
5d6bcd44
AT
523
524 p = strchr(argv[0],':');
525
526 if (p) {
9486289c
AT
527 if (p[1] == ':') {
528 *p = 0;
529 return start_socket_client(argv[0], p+2, argc-1, argv+1);
530 }
3591c066
AT
531
532 if (argc < 2) {
533 usage(FERROR);
534 exit_cleanup(1);
535 }
536
5d6bcd44
AT
537 am_sender = 0;
538 *p = 0;
539 shell_machine = argv[0];
540 shell_path = p+1;
541 argc--;
542 argv++;
543 } else {
544 am_sender = 1;
9486289c 545
5d6bcd44
AT
546 p = strchr(argv[argc-1],':');
547 if (!p) {
548 local_server = 1;
9486289c
AT
549 } else if (p[1] == ':') {
550 *p = 0;
551 return start_socket_client(argv[argc-1], p+2, argc-1, argv);
5d6bcd44 552 }
3591c066
AT
553
554 if (argc < 2) {
555 usage(FERROR);
556 exit_cleanup(1);
557 }
9486289c 558
5d6bcd44
AT
559 if (local_server) {
560 shell_machine = NULL;
561 shell_path = argv[argc-1];
562 } else {
563 *p = 0;
564 shell_machine = argv[argc-1];
565 shell_path = p+1;
566 }
567 argc--;
568 }
569
570 if (shell_machine) {
571 p = strchr(shell_machine,'@');
572 if (p) {
573 *p = 0;
574 shell_user = shell_machine;
575 shell_machine = p+1;
576 }
577 }
578
579 if (verbose > 3) {
9486289c 580 rprintf(FINFO,"cmd=%s machine=%s user=%s path=%s\n",
5d6bcd44
AT
581 shell_cmd?shell_cmd:"",
582 shell_machine?shell_machine:"",
583 shell_user?shell_user:"",
584 shell_path?shell_path:"");
585 }
586
587 if (!am_sender && argc != 1) {
588 usage(FERROR);
589 exit_cleanup(1);
590 }
591
592 pid = do_cmd(shell_cmd,shell_machine,shell_user,shell_path,&f_in,&f_out);
593
5d6bcd44 594#if HAVE_SETLINEBUF
9486289c
AT
595 setlinebuf(stdout);
596 setlinebuf(stderr);
5d6bcd44 597#endif
9486289c
AT
598
599 return client_run(f_in, f_out, pid, argc, argv);
5d6bcd44
AT
600}
601
366345fe 602
9486289c 603static void usage(int F)
c627d613 604{
9486289c 605 rprintf(F,"rsync version %s Copyright Andrew Tridgell and Paul Mackerras\n\n",
c627d613 606 VERSION);
9486289c
AT
607 rprintf(F,"Usage:\t%s [options] src user@host:dest\nOR",RSYNC_NAME);
608 rprintf(F,"\t%s [options] user@host:src dest\n\n",RSYNC_NAME);
609 rprintf(F,"Options:\n");
610 rprintf(F,"-v, --verbose increase verbosity\n");
611 rprintf(F,"-c, --checksum always checksum\n");
612 rprintf(F,"-a, --archive archive mode (same as -rlptDog)\n");
613 rprintf(F,"-r, --recursive recurse into directories\n");
614 rprintf(F,"-R, --relative use relative path names\n");
615 rprintf(F,"-b, --backup make backups (default ~ extension)\n");
616 rprintf(F,"-u, --update update only (don't overwrite newer files)\n");
617 rprintf(F,"-l, --links preserve soft links\n");
618 rprintf(F,"-L, --copy-links treat soft links like regular files\n");
619 rprintf(F,"-H, --hard-links preserve hard links\n");
620 rprintf(F,"-p, --perms preserve permissions\n");
621 rprintf(F,"-o, --owner preserve owner (root only)\n");
622 rprintf(F,"-g, --group preserve group\n");
623 rprintf(F,"-D, --devices preserve devices (root only)\n");
624 rprintf(F,"-t, --times preserve times\n");
625 rprintf(F,"-S, --sparse handle sparse files efficiently\n");
626 rprintf(F,"-n, --dry-run show what would have been transferred\n");
627 rprintf(F,"-W, --whole-file copy whole files, no incremental checks\n");
628 rprintf(F,"-x, --one-file-system don't cross filesystem boundaries\n");
629 rprintf(F,"-B, --block-size SIZE checksum blocking size\n");
630 rprintf(F,"-e, --rsh COMMAND specify rsh replacement\n");
631 rprintf(F," --rsync-path PATH specify path to rsync on the remote machine\n");
632 rprintf(F,"-C, --cvs-exclude auto ignore files in the same way CVS does\n");
633 rprintf(F," --delete delete files that don't exist on the sending side\n");
634 rprintf(F," --force force deletion of directories even if not empty\n");
635 rprintf(F," --numeric-ids don't map uid/gid values by user/group name\n");
636 rprintf(F," --timeout TIME set IO timeout in seconds\n");
637 rprintf(F,"-I, --ignore-times don't exclude files that match length and time\n");
638 rprintf(F,"-T --temp-dir DIR create temporary files in directory DIR\n");
639 rprintf(F,"-z, --compress compress file data\n");
640 rprintf(F," --exclude FILE exclude file FILE\n");
641 rprintf(F," --exclude-from FILE exclude files listed in FILE\n");
642 rprintf(F," --suffix SUFFIX override backup suffix\n");
643 rprintf(F," --version print version number\n");
644
645 rprintf(F,"\n");
646 rprintf(F,"the backup suffix defaults to %s\n",BACKUP_SUFFIX);
647 rprintf(F,"the block size defaults to %d\n",BLOCK_SIZE);
c627d613
AT
648}
649
650enum {OPT_VERSION,OPT_SUFFIX,OPT_SENDER,OPT_SERVER,OPT_EXCLUDE,
6ba9279f 651 OPT_EXCLUDE_FROM,OPT_DELETE,OPT_NUMERIC_IDS,OPT_RSYNC_PATH,
9486289c 652 OPT_FORCE,OPT_TIMEOUT,OPT_DAEMON};
c627d613 653
950ab32d 654static char *short_options = "oblLWHpguDCtcahvrRIxnSe:B:T:z";
c627d613
AT
655
656static struct option long_options[] = {
657 {"version", 0, 0, OPT_VERSION},
658 {"server", 0, 0, OPT_SERVER},
659 {"sender", 0, 0, OPT_SENDER},
660 {"delete", 0, 0, OPT_DELETE},
3cb6f5d6 661 {"force", 0, 0, OPT_FORCE},
f6c34742 662 {"numeric-ids", 0, 0, OPT_NUMERIC_IDS},
c627d613
AT
663 {"exclude", 1, 0, OPT_EXCLUDE},
664 {"exclude-from",1, 0, OPT_EXCLUDE_FROM},
665 {"rsync-path", 1, 0, OPT_RSYNC_PATH},
666 {"one-file-system",0, 0, 'x'},
667 {"ignore-times",0, 0, 'I'},
668 {"help", 0, 0, 'h'},
669 {"dry-run", 0, 0, 'n'},
dc5ddbcc 670 {"sparse", 0, 0, 'S'},
c627d613
AT
671 {"cvs-exclude", 0, 0, 'C'},
672 {"archive", 0, 0, 'a'},
673 {"checksum", 0, 0, 'c'},
674 {"backup", 0, 0, 'b'},
675 {"update", 0, 0, 'u'},
676 {"verbose", 0, 0, 'v'},
677 {"recursive", 0, 0, 'r'},
6574b4f7 678 {"relative", 0, 0, 'R'},
c627d613
AT
679 {"devices", 0, 0, 'D'},
680 {"perms", 0, 0, 'p'},
681 {"links", 0, 0, 'l'},
82306bf6
AT
682 {"copy-links", 0, 0, 'L'},
683 {"whole-file", 0, 0, 'W'},
dc5ddbcc 684 {"hard-links", 0, 0, 'H'},
c627d613
AT
685 {"owner", 0, 0, 'o'},
686 {"group", 0, 0, 'g'},
687 {"times", 0, 0, 't'},
688 {"rsh", 1, 0, 'e'},
689 {"suffix", 1, 0, OPT_SUFFIX},
690 {"block-size", 1, 0, 'B'},
6ba9279f 691 {"timeout", 1, 0, OPT_TIMEOUT},
950ab32d 692 {"temp-dir", 1, 0, 'T'},
861c20b4 693 {"compress", 0, 0, 'z'},
9486289c 694 {"daemon", 0, 0, OPT_DAEMON},
c627d613
AT
695 {0,0,0,0}};
696
82306bf6
AT
697RETSIGTYPE sigusr1_handler(int val) {
698 exit_cleanup(1);
699}
700
5d6bcd44 701
3591c066 702void parse_arguments(int argc, char *argv[])
c627d613 703{
c627d613
AT
704 int opt;
705 int option_index;
c627d613
AT
706
707 while ((opt = getopt_long(argc, argv,
708 short_options, long_options, &option_index))
709 != -1) {
710 switch (opt)
711 {
712 case OPT_VERSION:
713 printf("rsync version %s protocol version %d\n",
714 VERSION,PROTOCOL_VERSION);
34ccb63e 715 exit_cleanup(0);
c627d613
AT
716
717 case OPT_SUFFIX:
718 backup_suffix = optarg;
719 break;
720
721 case OPT_RSYNC_PATH:
722 rsync_path = optarg;
723 break;
724
725 case 'I':
726 ignore_times = 1;
727 break;
728
729 case 'x':
730 one_file_system=1;
731 break;
732
733 case OPT_DELETE:
734 delete_mode = 1;
735 break;
736
3cb6f5d6
AT
737 case OPT_FORCE:
738 force_delete = 1;
739 break;
740
f6c34742
AT
741 case OPT_NUMERIC_IDS:
742 numeric_ids = 1;
743 break;
744
c627d613
AT
745 case OPT_EXCLUDE:
746 add_exclude(optarg);
747 break;
748
749 case OPT_EXCLUDE_FROM:
750 add_exclude_file(optarg,1);
751 break;
752
753 case 'h':
dc5ddbcc 754 usage(FINFO);
34ccb63e 755 exit_cleanup(0);
c627d613
AT
756
757 case 'b':
758 make_backups=1;
759 break;
760
761 case 'n':
762 dry_run=1;
763 break;
764
dc5ddbcc
AT
765 case 'S':
766 sparse_files=1;
767 break;
768
c627d613
AT
769 case 'C':
770 cvs_exclude=1;
771 break;
772
773 case 'u':
774 update_only=1;
775 break;
776
c627d613
AT
777 case 'l':
778 preserve_links=1;
779 break;
dc5ddbcc 780
82306bf6
AT
781 case 'L':
782 copy_links=1;
783 break;
784
785 case 'W':
786 whole_file=1;
787 break;
788
dc5ddbcc
AT
789 case 'H':
790#if SUPPORT_HARD_LINKS
791 preserve_hard_links=1;
cbbe4892 792#else
9486289c 793 rprintf(FERROR,"ERROR: hard links not supported on this platform\n");
cbbe4892 794 exit_cleanup(1);
c627d613 795#endif
dc5ddbcc 796 break;
c627d613
AT
797
798 case 'p':
799 preserve_perms=1;
800 break;
801
802 case 'o':
7b8356d0 803 preserve_uid=1;
c627d613
AT
804 break;
805
806 case 'g':
807 preserve_gid=1;
808 break;
809
810 case 'D':
7b8356d0 811 preserve_devices=1;
c627d613
AT
812 break;
813
814 case 't':
815 preserve_times=1;
816 break;
817
818 case 'c':
819 always_checksum=1;
820 break;
821
822 case 'v':
823 verbose++;
824 break;
825
826 case 'a':
827 recurse=1;
828#if SUPPORT_LINKS
829 preserve_links=1;
830#endif
831 preserve_perms=1;
832 preserve_times=1;
833 preserve_gid=1;
7b8356d0 834 if (am_root) {
c627d613
AT
835 preserve_devices=1;
836 preserve_uid=1;
7b8356d0 837 }
c627d613
AT
838 break;
839
840 case OPT_SERVER:
841 am_server = 1;
842 break;
843
844 case OPT_SENDER:
845 if (!am_server) {
dc5ddbcc 846 usage(FERROR);
34ccb63e 847 exit_cleanup(1);
c627d613 848 }
366345fe 849 am_sender = 1;
c627d613
AT
850 break;
851
852 case 'r':
853 recurse = 1;
854 break;
855
6574b4f7
AT
856 case 'R':
857 relative_paths = 1;
858 break;
859
c627d613
AT
860 case 'e':
861 shell_cmd = optarg;
862 break;
863
864 case 'B':
865 block_size = atoi(optarg);
866 break;
867
6ba9279f
AT
868 case OPT_TIMEOUT:
869 io_timeout = atoi(optarg);
870 break;
871
950ab32d
AT
872 case 'T':
873 tmpdir = optarg;
874 break;
875
861c20b4
PM
876 case 'z':
877 do_compression = 1;
878 break;
879
9486289c
AT
880 case OPT_DAEMON:
881 am_daemon = 1;
882 break;
883
c627d613 884 default:
9486289c 885 /* rprintf(FERROR,"bad option -%c\n",opt); */
34ccb63e 886 exit_cleanup(1);
c627d613
AT
887 }
888 }
5d6bcd44
AT
889}
890
891int main(int argc,char *argv[])
892{
893
894 signal(SIGUSR1, sigusr1_handler);
895
896 starttime = time(NULL);
897 am_root = (getuid() == 0);
c627d613 898
5d6bcd44
AT
899 /* we set a 0 umask so that correct file permissions can be
900 carried across */
901 orig_umask = (int)umask(0);
902
903 parse_arguments(argc, argv);
904
f0fca04e
AT
905 argc -= optind;
906 argv += optind;
907 optind = 0;
c627d613 908
6b83141d
AT
909 signal(SIGCHLD,SIG_IGN);
910 signal(SIGINT,SIGNAL_CAST sig_int);
911 signal(SIGPIPE,SIGNAL_CAST sig_int);
912 signal(SIGHUP,SIGNAL_CAST sig_int);
913
f0fca04e
AT
914 if (am_daemon) {
915 return daemon_main();
916 }
917
c627d613
AT
918 if (dry_run)
919 verbose = MAX(verbose,1);
920
cbbe4892
AT
921#ifndef SUPPORT_LINKS
922 if (!am_server && preserve_links) {
9486289c 923 rprintf(FERROR,"ERROR: symbolic links not supported\n");
cbbe4892
AT
924 exit_cleanup(1);
925 }
926#endif
927
c627d613 928 if (am_server) {
9486289c 929 start_server(STDIN_FILENO, STDOUT_FILENO, argc, argv);
c627d613
AT
930 }
931
5d6bcd44 932 return start_client(argc, argv);
c627d613 933}
82306bf6 934