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