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