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