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