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