- always flush the IO write buffer when reading
[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,"    --include FILE       don't exclude file FILE\n");
113   rprintf(F,"    --include-from FILE  don't exclude files listed in FILE\n");
114   rprintf(F,"    --suffix SUFFIX      override backup suffix\n");  
115   rprintf(F,"    --version            print version number\n");  
116   rprintf(F,"    --daemon             run as a rsync daemon\n");  
117   rprintf(F,"    --config FILE        specify alternate rsyncd.conf file\n");  
118   rprintf(F,"    --port PORT          specify alternate rsyncd port number\n");  
119
120   rprintf(F,"\n");
121   rprintf(F,"the backup suffix defaults to %s\n",BACKUP_SUFFIX);
122   rprintf(F,"the block size defaults to %d\n",BLOCK_SIZE);  
123 }
124
125 enum {OPT_VERSION,OPT_SUFFIX,OPT_SENDER,OPT_SERVER,OPT_EXCLUDE,
126       OPT_EXCLUDE_FROM,OPT_DELETE,OPT_NUMERIC_IDS,OPT_RSYNC_PATH,
127       OPT_FORCE,OPT_TIMEOUT,OPT_DAEMON,OPT_CONFIG,OPT_PORT,
128       OPT_INCLUDE, OPT_INCLUDE_FROM};
129
130 static char *short_options = "oblLWHpguDCtcahvrRIxnSe:B:T:z";
131
132 static struct option long_options[] = {
133   {"version",     0,     0,    OPT_VERSION},
134   {"server",      0,     0,    OPT_SERVER},
135   {"sender",      0,     0,    OPT_SENDER},
136   {"delete",      0,     0,    OPT_DELETE},
137   {"force",       0,     0,    OPT_FORCE},
138   {"numeric-ids", 0,     0,    OPT_NUMERIC_IDS},
139   {"exclude",     1,     0,    OPT_EXCLUDE},
140   {"exclude-from",1,     0,    OPT_EXCLUDE_FROM},
141   {"include",     1,     0,    OPT_INCLUDE},
142   {"include-from",1,     0,    OPT_INCLUDE_FROM},
143   {"rsync-path",  1,     0,    OPT_RSYNC_PATH},
144   {"one-file-system",0,  0,    'x'},
145   {"ignore-times",0,     0,    'I'},
146   {"help",        0,     0,    'h'},
147   {"dry-run",     0,     0,    'n'},
148   {"sparse",      0,     0,    'S'},
149   {"cvs-exclude", 0,     0,    'C'},
150   {"archive",     0,     0,    'a'},
151   {"checksum",    0,     0,    'c'},
152   {"backup",      0,     0,    'b'},
153   {"update",      0,     0,    'u'},
154   {"verbose",     0,     0,    'v'},
155   {"recursive",   0,     0,    'r'},
156   {"relative",    0,     0,    'R'},
157   {"devices",     0,     0,    'D'},
158   {"perms",       0,     0,    'p'},
159   {"links",       0,     0,    'l'},
160   {"copy-links",  0,     0,    'L'},
161   {"whole-file",  0,     0,    'W'},
162   {"hard-links",  0,     0,    'H'},
163   {"owner",       0,     0,    'o'},
164   {"group",       0,     0,    'g'},
165   {"times",       0,     0,    't'},
166   {"rsh",         1,     0,    'e'},
167   {"suffix",      1,     0,    OPT_SUFFIX},
168   {"block-size",  1,     0,    'B'},
169   {"timeout",     1,     0,    OPT_TIMEOUT},
170   {"temp-dir",    1,     0,    'T'},
171   {"compress",    0,     0,    'z'},
172   {"daemon",      0,     0,    OPT_DAEMON},
173   {"config",      1,     0,    OPT_CONFIG},
174   {"port",        1,     0,    OPT_PORT},
175   {0,0,0,0}};
176
177 void parse_arguments(int argc, char *argv[])
178 {
179     int opt;
180     int option_index;
181
182     while ((opt = getopt_long(argc, argv, 
183                               short_options, long_options, &option_index)) 
184            != -1) {
185       switch (opt) 
186         {
187         case OPT_VERSION:
188           printf("rsync version %s  protocol version %d\n",
189                  VERSION,PROTOCOL_VERSION);
190           exit_cleanup(0);
191
192         case OPT_SUFFIX:
193           backup_suffix = optarg;
194           break;
195
196         case OPT_RSYNC_PATH:
197           rsync_path = optarg;
198           break;
199
200         case 'I':
201           ignore_times = 1;
202           break;
203
204         case 'x':
205           one_file_system=1;
206           break;
207
208         case OPT_DELETE:
209           delete_mode = 1;
210           break;
211
212         case OPT_FORCE:
213           force_delete = 1;
214           break;
215
216         case OPT_NUMERIC_IDS:
217           numeric_ids = 1;
218           break;
219
220         case OPT_EXCLUDE:
221           add_exclude(optarg, 0);
222           break;
223
224         case OPT_INCLUDE:
225           add_exclude(optarg, 1);
226           break;
227
228         case OPT_EXCLUDE_FROM:
229           add_exclude_file(optarg,1, 0);
230           break;
231
232         case OPT_INCLUDE_FROM:
233           add_exclude_file(optarg,1, 1);
234           break;
235
236         case 'h':
237           usage(FINFO);
238           exit_cleanup(0);
239
240         case 'b':
241           make_backups=1;
242           break;
243
244         case 'n':
245           dry_run=1;
246           break;
247
248         case 'S':
249           sparse_files=1;
250           break;
251
252         case 'C':
253           cvs_exclude=1;
254           break;
255
256         case 'u':
257           update_only=1;
258           break;
259
260         case 'l':
261           preserve_links=1;
262           break;
263
264         case 'L':
265           copy_links=1;
266           break;
267
268         case 'W':
269           whole_file=1;
270           break;
271
272         case 'H':
273 #if SUPPORT_HARD_LINKS
274           preserve_hard_links=1;
275 #else 
276           rprintf(FERROR,"ERROR: hard links not supported on this platform\n");
277           exit_cleanup(1);
278 #endif
279           break;
280
281         case 'p':
282           preserve_perms=1;
283           break;
284
285         case 'o':
286           preserve_uid=1;
287           break;
288
289         case 'g':
290           preserve_gid=1;
291           break;
292
293         case 'D':
294           preserve_devices=1;
295           break;
296
297         case 't':
298           preserve_times=1;
299           break;
300
301         case 'c':
302           always_checksum=1;
303           break;
304
305         case 'v':
306           verbose++;
307           break;
308
309         case 'a':
310           recurse=1;
311 #if SUPPORT_LINKS
312           preserve_links=1;
313 #endif
314           preserve_perms=1;
315           preserve_times=1;
316           preserve_gid=1;
317           if (am_root) {
318             preserve_devices=1;
319             preserve_uid=1;
320           }
321           break;
322
323         case OPT_SERVER:
324           am_server = 1;
325           break;
326
327         case OPT_SENDER:
328           if (!am_server) {
329             usage(FERROR);
330             exit_cleanup(1);
331           }
332           am_sender = 1;
333           break;
334
335         case 'r':
336           recurse = 1;
337           break;
338
339         case 'R':
340           relative_paths = 1;
341           break;
342
343         case 'e':
344           shell_cmd = optarg;
345           break;
346
347         case 'B':
348           block_size = atoi(optarg);
349           break;
350
351         case OPT_TIMEOUT:
352           io_timeout = atoi(optarg);
353           break;
354
355         case 'T':
356                 tmpdir = optarg;
357                 break;
358
359         case 'z':
360           do_compression = 1;
361           break;
362
363         case OPT_DAEMON:
364                 am_daemon = 1;
365                 break;
366
367         case OPT_CONFIG:
368                 config_file = optarg;
369                 break;
370
371         case OPT_PORT:
372                 rsync_port = atoi(optarg);
373                 break;
374
375         default:
376           /* rprintf(FERROR,"bad option -%c\n",opt); */
377           exit_cleanup(1);
378         }
379     }
380 }
381
382
383 void server_options(char **args,int *argc)
384 {
385   int ac = *argc;
386   static char argstr[50];
387   static char bsize[30];
388   static char iotime[30];
389   int i, x;
390
391   args[ac++] = "--server";
392
393   if (!am_sender)
394     args[ac++] = "--sender";
395
396   x = 1;
397   argstr[0] = '-';
398   for (i=0;i<verbose;i++)
399     argstr[x++] = 'v';
400   if (make_backups)
401     argstr[x++] = 'b';
402   if (update_only)
403     argstr[x++] = 'u';
404   if (dry_run)
405     argstr[x++] = 'n';
406   if (preserve_links)
407     argstr[x++] = 'l';
408   if (copy_links)
409     argstr[x++] = 'L';
410   if (whole_file)
411     argstr[x++] = 'W';
412   if (preserve_hard_links)
413     argstr[x++] = 'H';
414   if (preserve_uid)
415     argstr[x++] = 'o';
416   if (preserve_gid)
417     argstr[x++] = 'g';
418   if (preserve_devices)
419     argstr[x++] = 'D';
420   if (preserve_times)
421     argstr[x++] = 't';
422   if (preserve_perms)
423     argstr[x++] = 'p';
424   if (recurse)
425     argstr[x++] = 'r';
426   if (always_checksum)
427     argstr[x++] = 'c';
428   if (cvs_exclude)
429     argstr[x++] = 'C';
430   if (ignore_times)
431     argstr[x++] = 'I';
432   if (relative_paths)
433     argstr[x++] = 'R';
434   if (one_file_system)
435     argstr[x++] = 'x';
436   if (sparse_files)
437     argstr[x++] = 'S';
438   if (do_compression)
439     argstr[x++] = 'z';
440   argstr[x] = 0;
441
442   if (x != 1) args[ac++] = argstr;
443
444   if (block_size != BLOCK_SIZE) {
445     sprintf(bsize,"-B%d",block_size);
446     args[ac++] = bsize;
447   }    
448
449   if (io_timeout) {
450     sprintf(iotime,"--timeout=%d",io_timeout);
451     args[ac++] = iotime;
452   }    
453
454   if (strcmp(backup_suffix, BACKUP_SUFFIX)) {
455           args[ac++] = "--suffix";
456           args[ac++] = backup_suffix;
457   }
458
459   if (delete_mode)
460     args[ac++] = "--delete";
461
462   if (force_delete)
463     args[ac++] = "--force";
464
465   if (numeric_ids)
466     args[ac++] = "--numeric-ids";
467
468   if (tmpdir) {
469           args[ac++] = "--temp-dir";
470           args[ac++] = tmpdir;
471   }
472
473   *argc = ac;
474 }
475