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