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