updated test suite from Phil.
[rsync/rsync.git] / options.c
CommitLineData
7a6421fa
AT
1/*
2 Copyright (C) Andrew Tridgell 1998
3
4 This program is free software; you can redistribute it and/or modify
5 it under the terms of the GNU General Public License as published by
6 the Free Software Foundation; either version 2 of the License, or
7 (at your option) any later version.
8
9 This program is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 GNU General Public License for more details.
13
14 You should have received a copy of the GNU General Public License
15 along with this program; if not, write to the Free Software
16 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
17*/
18
19/* options parsing code */
20
21#include "rsync.h"
22
23
24int make_backups = 0;
25int whole_file = 0;
26int copy_links = 0;
27int preserve_links = 0;
28int preserve_hard_links = 0;
29int preserve_perms = 0;
30int preserve_devices = 0;
31int preserve_uid = 0;
32int preserve_gid = 0;
33int preserve_times = 0;
34int update_only = 0;
35int cvs_exclude = 0;
36int dry_run=0;
37int local_server=0;
38int ignore_times=0;
39int delete_mode=0;
b33b791e 40int delete_excluded=0;
7a6421fa
AT
41int one_file_system=0;
42int remote_version=0;
43int sparse_files=0;
44int do_compression=0;
45int am_root=0;
46int orig_umask=0;
47int relative_paths=0;
48int numeric_ids = 0;
49int force_delete = 0;
50int io_timeout = 0;
51int io_error = 0;
52int read_only = 0;
53int module_id = -1;
54int am_server = 0;
55int am_sender=0;
56int recurse = 0;
57int am_daemon=0;
a800434a 58int do_stats=0;
eb86d661 59int do_progress=0;
c95da96a 60int keep_partial=0;
d853783f 61int safe_symlinks=0;
b5313607 62int copy_unsafe_links=0;
7a6421fa 63int block_size=BLOCK_SIZE;
f83f0548 64int size_only=0;
7a6421fa
AT
65
66char *backup_suffix = BACKUP_SUFFIX;
67char *tmpdir = NULL;
375a4556 68char *compare_dest = NULL;
7a6421fa
AT
69char *config_file = RSYNCD_CONF;
70char *shell_cmd = NULL;
b6062654 71char *log_format = NULL;
65575e96 72char *password_file = NULL;
7a6421fa
AT
73char *rsync_path = RSYNC_NAME;
74int rsync_port = RSYNC_PORT;
75
76int verbose = 0;
b86f0cef 77int quiet = 0;
7a6421fa 78int always_checksum = 0;
f7632fc6 79int list_only = 0;
7a6421fa
AT
80
81void usage(int F)
82{
83 rprintf(F,"rsync version %s Copyright Andrew Tridgell and Paul Mackerras\n\n",
84 VERSION);
704f908e
AT
85
86 rprintf(F,"rsync is a file transfer program capable of efficient remote update\nvia a fast differencing algorithm.\n\n");
87
9ef53907 88 rprintf(F,"Usage: rsync [OPTION]... SRC [SRC]... [USER@]HOST:DEST\n");
704f908e 89 rprintf(F," or rsync [OPTION]... [USER@]HOST:SRC DEST\n");
9ef53907 90 rprintf(F," or rsync [OPTION]... SRC [SRC]... DEST\n");
14d43f1f 91 rprintf(F," or rsync [OPTION]... [USER@]HOST::SRC [DEST]\n");
9ef53907 92 rprintf(F," or rsync [OPTION]... SRC [SRC]... [USER@]HOST::DEST\n");
14d43f1f 93 rprintf(F," or rsync [OPTION]... rsync://[USER@]HOST[:PORT]/SRC [DEST]\n");
9ef53907
DD
94 rprintf(F,"SRC on single-colon remote HOST will be expanded by remote shell\n");
95 rprintf(F,"SRC on server remote HOST may contain shell wildcards or multiple\n");
96 rprintf(F," sources separated by space as long as they have same top-level\n");
704f908e
AT
97 rprintf(F,"\nOptions\n");
98 rprintf(F," -v, --verbose increase verbosity\n");
b86f0cef 99 rprintf(F," -q, --quiet decrease verbosity\n");
704f908e
AT
100 rprintf(F," -c, --checksum always checksum\n");
101 rprintf(F," -a, --archive archive mode\n");
102 rprintf(F," -r, --recursive recurse into directories\n");
103 rprintf(F," -R, --relative use relative path names\n");
9ef53907
DD
104 rprintf(F," -b, --backup make backups (default %s suffix)\n",BACKUP_SUFFIX);
105 rprintf(F," --suffix=SUFFIX override backup suffix\n");
704f908e
AT
106 rprintf(F," -u, --update update only (don't overwrite newer files)\n");
107 rprintf(F," -l, --links preserve soft links\n");
108 rprintf(F," -L, --copy-links treat soft links like regular files\n");
b5313607 109 rprintf(F," --copy-unsafe-links copy links outside the source tree\n");
d853783f 110 rprintf(F," --safe-links ignore links outside the destination tree\n");
704f908e
AT
111 rprintf(F," -H, --hard-links preserve hard links\n");
112 rprintf(F," -p, --perms preserve permissions\n");
113 rprintf(F," -o, --owner preserve owner (root only)\n");
114 rprintf(F," -g, --group preserve group\n");
115 rprintf(F," -D, --devices preserve devices (root only)\n");
116 rprintf(F," -t, --times preserve times\n");
117 rprintf(F," -S, --sparse handle sparse files efficiently\n");
118 rprintf(F," -n, --dry-run show what would have been transferred\n");
119 rprintf(F," -W, --whole-file copy whole files, no incremental checks\n");
120 rprintf(F," -x, --one-file-system don't cross filesystem boundaries\n");
9ef53907 121 rprintf(F," -B, --block-size=SIZE checksum blocking size (default %d)\n",BLOCK_SIZE);
704f908e
AT
122 rprintf(F," -e, --rsh=COMMAND specify rsh replacement\n");
123 rprintf(F," --rsync-path=PATH specify path to rsync on the remote machine\n");
124 rprintf(F," -C, --cvs-exclude auto ignore files in the same way CVS does\n");
125 rprintf(F," --delete delete files that don't exist on the sending side\n");
b33b791e 126 rprintf(F," --delete-excluded also delete excluded files on the receiving side\n");
c95da96a 127 rprintf(F," --partial keep partially transferred files\n");
704f908e
AT
128 rprintf(F," --force force deletion of directories even if not empty\n");
129 rprintf(F," --numeric-ids don't map uid/gid values by user/group name\n");
130 rprintf(F," --timeout=TIME set IO timeout in seconds\n");
131 rprintf(F," -I, --ignore-times don't exclude files that match length and time\n");
f83f0548 132 rprintf(F," --size-only only use file size when determining if a file should be transferred\n");
704f908e 133 rprintf(F," -T --temp-dir=DIR create temporary files in directory DIR\n");
375a4556 134 rprintf(F," --compare-dest=DIR also compare destination files relative to DIR\n");
704f908e 135 rprintf(F," -z, --compress compress file data\n");
2acf81eb 136 rprintf(F," --exclude=PATTERN exclude files matching PATTERN\n");
858fb9eb 137 rprintf(F," --exclude-from=FILE exclude patterns listed in FILE\n");
2acf81eb 138 rprintf(F," --include=PATTERN don't exclude files matching PATTERN\n");
858fb9eb 139 rprintf(F," --include-from=FILE don't exclude patterns listed in FILE\n");
704f908e
AT
140 rprintf(F," --version print version number\n");
141 rprintf(F," --daemon run as a rsync daemon\n");
142 rprintf(F," --config=FILE specify alternate rsyncd.conf file\n");
143 rprintf(F," --port=PORT specify alternate rsyncd port number\n");
144 rprintf(F," --stats give some file transfer stats\n");
eb86d661 145 rprintf(F," --progress show progress during transfer\n");
b6062654 146 rprintf(F," --log-format=FORMAT log file transfers using specified format\n");
65575e96 147 rprintf(F," --password-file=FILE get password from FILE\n");
704f908e 148 rprintf(F," -h, --help show this help screen\n");
7a6421fa
AT
149
150 rprintf(F,"\n");
b72f24c7
AT
151
152 rprintf(F,"\nPlease see the rsync(1) and rsyncd.conf(5) man pages for full documentation\n");
9e3c856a 153 rprintf(F,"See http://rsync.samba.org/ for updates and bug reports\n");
7a6421fa
AT
154}
155
b33b791e
DD
156enum {OPT_VERSION, OPT_SUFFIX, OPT_SENDER, OPT_SERVER, OPT_EXCLUDE,
157 OPT_EXCLUDE_FROM, OPT_DELETE, OPT_DELETE_EXCLUDED, OPT_NUMERIC_IDS,
158 OPT_RSYNC_PATH, OPT_FORCE, OPT_TIMEOUT, OPT_DAEMON, OPT_CONFIG, OPT_PORT,
d853783f 159 OPT_INCLUDE, OPT_INCLUDE_FROM, OPT_STATS, OPT_PARTIAL, OPT_PROGRESS,
b5313607 160 OPT_COPY_UNSAFE_LINKS, OPT_SAFE_LINKS, OPT_COMPARE_DEST,
f83f0548 161 OPT_LOG_FORMAT, OPT_PASSWORD_FILE, OPT_SIZE_ONLY};
7a6421fa 162
b86f0cef 163static char *short_options = "oblLWHpguDCtcahvqrRIxnSe:B:T:z";
7a6421fa
AT
164
165static struct option long_options[] = {
166 {"version", 0, 0, OPT_VERSION},
167 {"server", 0, 0, OPT_SERVER},
168 {"sender", 0, 0, OPT_SENDER},
169 {"delete", 0, 0, OPT_DELETE},
b33b791e 170 {"delete-excluded", 0, 0, OPT_DELETE_EXCLUDED},
7a6421fa
AT
171 {"force", 0, 0, OPT_FORCE},
172 {"numeric-ids", 0, 0, OPT_NUMERIC_IDS},
173 {"exclude", 1, 0, OPT_EXCLUDE},
174 {"exclude-from",1, 0, OPT_EXCLUDE_FROM},
2b6b4d53
AT
175 {"include", 1, 0, OPT_INCLUDE},
176 {"include-from",1, 0, OPT_INCLUDE_FROM},
7a6421fa 177 {"rsync-path", 1, 0, OPT_RSYNC_PATH},
65575e96 178 {"password-file", 1, 0, OPT_PASSWORD_FILE},
7a6421fa
AT
179 {"one-file-system",0, 0, 'x'},
180 {"ignore-times",0, 0, 'I'},
f83f0548 181 {"size-only", 0, 0, OPT_SIZE_ONLY},
7a6421fa
AT
182 {"help", 0, 0, 'h'},
183 {"dry-run", 0, 0, 'n'},
184 {"sparse", 0, 0, 'S'},
185 {"cvs-exclude", 0, 0, 'C'},
186 {"archive", 0, 0, 'a'},
187 {"checksum", 0, 0, 'c'},
188 {"backup", 0, 0, 'b'},
189 {"update", 0, 0, 'u'},
190 {"verbose", 0, 0, 'v'},
b86f0cef 191 {"quiet", 0, 0, 'q'},
7a6421fa
AT
192 {"recursive", 0, 0, 'r'},
193 {"relative", 0, 0, 'R'},
194 {"devices", 0, 0, 'D'},
195 {"perms", 0, 0, 'p'},
196 {"links", 0, 0, 'l'},
197 {"copy-links", 0, 0, 'L'},
b5313607 198 {"copy-unsafe-links", 0, 0, OPT_COPY_UNSAFE_LINKS},
d853783f 199 {"safe-links", 0, 0, OPT_SAFE_LINKS},
7a6421fa
AT
200 {"whole-file", 0, 0, 'W'},
201 {"hard-links", 0, 0, 'H'},
202 {"owner", 0, 0, 'o'},
203 {"group", 0, 0, 'g'},
204 {"times", 0, 0, 't'},
205 {"rsh", 1, 0, 'e'},
206 {"suffix", 1, 0, OPT_SUFFIX},
207 {"block-size", 1, 0, 'B'},
208 {"timeout", 1, 0, OPT_TIMEOUT},
209 {"temp-dir", 1, 0, 'T'},
375a4556 210 {"compare-dest", 1, 0, OPT_COMPARE_DEST},
7a6421fa
AT
211 {"compress", 0, 0, 'z'},
212 {"daemon", 0, 0, OPT_DAEMON},
a800434a 213 {"stats", 0, 0, OPT_STATS},
eb86d661 214 {"progress", 0, 0, OPT_PROGRESS},
c95da96a 215 {"partial", 0, 0, OPT_PARTIAL},
7a6421fa
AT
216 {"config", 1, 0, OPT_CONFIG},
217 {"port", 1, 0, OPT_PORT},
b6062654 218 {"log-format", 1, 0, OPT_LOG_FORMAT},
7a6421fa
AT
219 {0,0,0,0}};
220
b11ed3b1 221
cd8185f2
AT
222static char err_buf[100];
223
224void option_error(void)
225{
226 if (err_buf[0]) {
227 rprintf(FLOG,"%s", err_buf);
228 rprintf(FERROR,"%s", err_buf);
229 } else {
230 rprintf(FLOG,"Error parsing options - unsupported option?\n");
231 rprintf(FERROR,"Error parsing options - unsupported option?\n");
232 }
233 exit_cleanup(RERR_UNSUPPORTED);
234}
235
236/* check to see if we should refuse this option */
237static int check_refuse_options(char *ref, int opt)
238{
239 int i, len;
240 char *p;
241 const char *name;
242
243 for (i=0; long_options[i].name; i++) {
244 if (long_options[i].val == opt) break;
245 }
246
247 if (!long_options[i].name) return 0;
248
249 name = long_options[i].name;
250 len = strlen(name);
251
252 while ((p = strstr(ref,name))) {
055af776
AT
253 if ((p==ref || p[-1]==' ') &&
254 (p[len] == ' ' || p[len] == 0)) {
cd8185f2
AT
255 slprintf(err_buf,sizeof(err_buf),
256 "The '%s' option is not supported by this server\n", name);
257 return 1;
258 }
259 ref += len;
260 }
261 return 0;
262}
263
264
b86f0cef 265int parse_arguments(int argc, char *argv[], int frommain)
7a6421fa 266{
d853783f
AT
267 int opt;
268 int option_index;
cd8185f2 269 char *ref = lp_refuse_options(module_id);
d853783f
AT
270
271 while ((opt = getopt_long(argc, argv,
272 short_options, long_options, &option_index))
273 != -1) {
cd8185f2
AT
274
275 if (ref) {
276 if (check_refuse_options(ref, opt)) return 0;
277 }
278
d853783f
AT
279 switch (opt) {
280 case OPT_VERSION:
281 rprintf(FINFO,"rsync version %s protocol version %d\n\n",
282 VERSION,PROTOCOL_VERSION);
283 rprintf(FINFO,"Written by Andrew Tridgell and Paul Mackerras\n");
284 exit_cleanup(0);
285
286 case OPT_SUFFIX:
287 backup_suffix = optarg;
288 break;
289
290 case OPT_RSYNC_PATH:
291 rsync_path = optarg;
292 break;
65575e96
AT
293
294 case OPT_PASSWORD_FILE:
295 password_file =optarg;
296 break;
f83f0548 297
d853783f
AT
298 case 'I':
299 ignore_times = 1;
300 break;
301
f83f0548
AT
302 case OPT_SIZE_ONLY:
303 size_only = 1;
304 break;
305
d853783f
AT
306 case 'x':
307 one_file_system=1;
308 break;
309
310 case OPT_DELETE:
311 delete_mode = 1;
312 break;
313
b33b791e
DD
314 case OPT_DELETE_EXCLUDED:
315 delete_excluded = 1;
316 delete_mode = 1;
317 break;
318
d853783f
AT
319 case OPT_FORCE:
320 force_delete = 1;
321 break;
322
323 case OPT_NUMERIC_IDS:
324 numeric_ids = 1;
325 break;
326
327 case OPT_EXCLUDE:
328 add_exclude(optarg, 0);
329 break;
330
331 case OPT_INCLUDE:
332 add_exclude(optarg, 1);
333 break;
334
335 case OPT_EXCLUDE_FROM:
336 add_exclude_file(optarg,1, 0);
337 break;
338
339 case OPT_INCLUDE_FROM:
340 add_exclude_file(optarg,1, 1);
341 break;
342
b5313607
DD
343 case OPT_COPY_UNSAFE_LINKS:
344 copy_unsafe_links=1;
345 break;
346
d853783f
AT
347 case OPT_SAFE_LINKS:
348 safe_symlinks=1;
349 break;
350
351 case 'h':
352 usage(FINFO);
353 exit_cleanup(0);
354
355 case 'b':
356 make_backups=1;
357 break;
358
359 case 'n':
360 dry_run=1;
361 break;
362
363 case 'S':
364 sparse_files=1;
365 break;
366
367 case 'C':
368 cvs_exclude=1;
369 break;
370
371 case 'u':
372 update_only=1;
373 break;
374
375 case 'l':
376 preserve_links=1;
377 break;
378
379 case 'L':
380 copy_links=1;
381 break;
382
383 case 'W':
384 whole_file=1;
385 break;
386
387 case 'H':
7a6421fa 388#if SUPPORT_HARD_LINKS
d853783f 389 preserve_hard_links=1;
7a6421fa 390#else
cd8185f2 391 slprintf(err_buf,sizeof(err_buf),"hard links are not supported on this server\n");
d853783f 392 rprintf(FERROR,"ERROR: hard links not supported on this platform\n");
b11ed3b1 393 return 0;
7a6421fa 394#endif
d853783f 395 break;
7a6421fa 396
d853783f
AT
397 case 'p':
398 preserve_perms=1;
399 break;
7a6421fa 400
d853783f
AT
401 case 'o':
402 preserve_uid=1;
403 break;
7a6421fa 404
d853783f
AT
405 case 'g':
406 preserve_gid=1;
407 break;
7a6421fa 408
d853783f
AT
409 case 'D':
410 preserve_devices=1;
411 break;
7a6421fa 412
d853783f
AT
413 case 't':
414 preserve_times=1;
415 break;
7a6421fa 416
d853783f
AT
417 case 'c':
418 always_checksum=1;
419 break;
7a6421fa 420
d853783f
AT
421 case 'v':
422 verbose++;
423 break;
7a6421fa 424
b86f0cef
DD
425 case 'q':
426 if (frommain) quiet++;
427 break;
428
d853783f
AT
429 case 'a':
430 recurse=1;
7a6421fa 431#if SUPPORT_LINKS
d853783f 432 preserve_links=1;
7a6421fa 433#endif
d853783f
AT
434 preserve_perms=1;
435 preserve_times=1;
436 preserve_gid=1;
437 if (am_root) {
438 preserve_devices=1;
439 preserve_uid=1;
440 }
441 break;
442
443 case OPT_SERVER:
444 am_server = 1;
445 break;
446
447 case OPT_SENDER:
448 if (!am_server) {
449 usage(FERROR);
65417579 450 exit_cleanup(RERR_SYNTAX);
d853783f
AT
451 }
452 am_sender = 1;
453 break;
454
455 case 'r':
456 recurse = 1;
457 break;
458
459 case 'R':
460 relative_paths = 1;
461 break;
462
463 case 'e':
464 shell_cmd = optarg;
465 break;
466
467 case 'B':
468 block_size = atoi(optarg);
469 break;
470
471 case OPT_TIMEOUT:
472 io_timeout = atoi(optarg);
473 break;
474
475 case 'T':
476 tmpdir = optarg;
477 break;
478
375a4556
DD
479 case OPT_COMPARE_DEST:
480 compare_dest = optarg;
481 break;
482
d853783f
AT
483 case 'z':
484 do_compression = 1;
485 break;
486
487 case OPT_DAEMON:
488 am_daemon = 1;
489 break;
490
491 case OPT_STATS:
492 do_stats = 1;
493 break;
494
495 case OPT_PROGRESS:
496 do_progress = 1;
497 break;
498
499 case OPT_PARTIAL:
500 keep_partial = 1;
501 break;
502
503 case OPT_CONFIG:
504 config_file = optarg;
505 break;
506
507 case OPT_PORT:
508 rsync_port = atoi(optarg);
509 break;
510
b6062654
AT
511 case OPT_LOG_FORMAT:
512 log_format = optarg;
513 break;
514
d853783f 515 default:
cd8185f2 516 slprintf(err_buf,sizeof(err_buf),"unrecognised option\n");
b11ed3b1 517 return 0;
d853783f 518 }
7a6421fa 519 }
b11ed3b1 520 return 1;
7a6421fa
AT
521}
522
523
524void server_options(char **args,int *argc)
525{
d853783f
AT
526 int ac = *argc;
527 static char argstr[50];
528 static char bsize[30];
529 static char iotime[30];
530 int i, x;
531
532 args[ac++] = "--server";
533
534 if (!am_sender)
535 args[ac++] = "--sender";
536
537 x = 1;
538 argstr[0] = '-';
539 for (i=0;i<verbose;i++)
540 argstr[x++] = 'v';
b86f0cef 541 /* the -q option is intentionally left out */
d853783f
AT
542 if (make_backups)
543 argstr[x++] = 'b';
544 if (update_only)
545 argstr[x++] = 'u';
546 if (dry_run)
547 argstr[x++] = 'n';
548 if (preserve_links)
549 argstr[x++] = 'l';
550 if (copy_links)
551 argstr[x++] = 'L';
552 if (whole_file)
553 argstr[x++] = 'W';
554 if (preserve_hard_links)
555 argstr[x++] = 'H';
556 if (preserve_uid)
557 argstr[x++] = 'o';
558 if (preserve_gid)
559 argstr[x++] = 'g';
560 if (preserve_devices)
561 argstr[x++] = 'D';
562 if (preserve_times)
563 argstr[x++] = 't';
564 if (preserve_perms)
565 argstr[x++] = 'p';
566 if (recurse)
567 argstr[x++] = 'r';
568 if (always_checksum)
569 argstr[x++] = 'c';
570 if (cvs_exclude)
571 argstr[x++] = 'C';
572 if (ignore_times)
573 argstr[x++] = 'I';
574 if (relative_paths)
575 argstr[x++] = 'R';
576 if (one_file_system)
577 argstr[x++] = 'x';
578 if (sparse_files)
579 argstr[x++] = 'S';
580 if (do_compression)
581 argstr[x++] = 'z';
582 argstr[x] = 0;
583
584 if (x != 1) args[ac++] = argstr;
585
586 if (block_size != BLOCK_SIZE) {
587 sprintf(bsize,"-B%d",block_size);
588 args[ac++] = bsize;
589 }
590
591 if (io_timeout) {
592 sprintf(iotime,"--timeout=%d",io_timeout);
593 args[ac++] = iotime;
594 }
595
596 if (strcmp(backup_suffix, BACKUP_SUFFIX)) {
597 args[ac++] = "--suffix";
598 args[ac++] = backup_suffix;
599 }
600
b33b791e 601 if (delete_mode && !delete_excluded)
d853783f
AT
602 args[ac++] = "--delete";
603
b33b791e
DD
604 if (delete_excluded)
605 args[ac++] = "--delete-excluded";
606
f83f0548
AT
607 if (size_only)
608 args[ac++] = "--size-only";
609
d853783f
AT
610 if (keep_partial)
611 args[ac++] = "--partial";
612
613 if (force_delete)
614 args[ac++] = "--force";
615
b5313607
DD
616 if (copy_unsafe_links)
617 args[ac++] = "--copy-unsafe-links";
618
d853783f
AT
619 if (safe_symlinks)
620 args[ac++] = "--safe-links";
621
622 if (numeric_ids)
623 args[ac++] = "--numeric-ids";
624
625 if (tmpdir) {
626 args[ac++] = "--temp-dir";
627 args[ac++] = tmpdir;
628 }
629
375a4556
DD
630 if (compare_dest && am_sender) {
631 /* the server only needs this option if it is not the sender,
632 * and it may be an older version that doesn't know this
633 * option, so don't send it if client is the sender.
634 */
635 args[ac++] = "--compare-dest";
636 args[ac++] = compare_dest;
637 }
638
639
d853783f 640 *argc = ac;
7a6421fa
AT
641}
642