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