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