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