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