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