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