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