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