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