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