Tweaked a couple lines in the code that handles --version.
[rsync/rsync.git] / options.c
1 /*  -*- c-file-style: "linux" -*-
2  *
3  * Copyright (C) 1998-2001 by Andrew Tridgell <tridge@samba.org>
4  * Copyright (C) 2000, 2001, 2002 by Martin Pool <mbp@samba.org>
5  *
6  * This program is free software; you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License as published by
8  * the Free Software Foundation; either version 2 of the License, or
9  * (at your option) any later version.
10  *
11  * This program is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14  * GNU General Public License for more details.
15  *
16  * You should have received a copy of the GNU General Public License
17  * along with this program; if not, write to the Free Software
18  * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
19  */
20
21 #include "rsync.h"
22 #include <popt.h>
23 #include "zlib/zlib.h"
24
25 extern int module_id;
26 extern int sanitize_paths;
27 extern struct filter_list_struct filter_list;
28 extern struct filter_list_struct server_filter_list;
29
30 int make_backups = 0;
31
32 /**
33  * If 1, send the whole file as literal data rather than trying to
34  * create an incremental diff.
35  *
36  * If -1, then look at whether we're local or remote and go by that.
37  *
38  * @sa disable_deltas_p()
39  **/
40 int whole_file = -1;
41
42 int append_mode = 0;
43 int keep_dirlinks = 0;
44 int copy_links = 0;
45 int preserve_links = 0;
46 int preserve_hard_links = 0;
47 int preserve_perms = 0;
48 int preserve_executability = 0;
49 int preserve_devices = 0;
50 int preserve_specials = 0;
51 int preserve_uid = 0;
52 int preserve_gid = 0;
53 int preserve_times = 0;
54 int omit_dir_times = 0;
55 int update_only = 0;
56 int cvs_exclude = 0;
57 int dry_run = 0;
58 int do_xfers = 1;
59 int ignore_times = 0;
60 int saw_delete_opt = 0;
61 int saw_delete_excluded_opt = 0;
62 int delete_mode = 0;
63 int delete_during = 0;
64 int delete_before = 0;
65 int delete_after = 0;
66 int delete_excluded = 0;
67 int remove_sent_files = 0;
68 int one_file_system = 0;
69 int protocol_version = PROTOCOL_VERSION;
70 int sparse_files = 0;
71 int do_compression = 0;
72 int def_compress_level = Z_DEFAULT_COMPRESSION;
73 int am_root = 0;
74 int am_server = 0;
75 int am_sender = 0;
76 int am_generator = 0;
77 int am_starting_up = 1;
78 int orig_umask = 0;
79 int relative_paths = -1;
80 int implied_dirs = 1;
81 int numeric_ids = 0;
82 int force_delete = 0;
83 int io_timeout = 0;
84 int allowed_lull = 0;
85 int prune_empty_dirs = 0;
86 char *files_from = NULL;
87 int filesfrom_fd = -1;
88 char *filesfrom_host = NULL;
89 int eol_nulls = 0;
90 int human_readable = 0;
91 int recurse = 0;
92 int xfer_dirs = -1;
93 int am_daemon = 0;
94 int daemon_over_rsh = 0;
95 int do_stats = 0;
96 int do_progress = 0;
97 int keep_partial = 0;
98 int safe_symlinks = 0;
99 int copy_unsafe_links = 0;
100 int size_only = 0;
101 int daemon_bwlimit = 0;
102 int bwlimit = 0;
103 int fuzzy_basis = 0;
104 size_t bwlimit_writemax = 0;
105 int ignore_existing = 0;
106 int ignore_non_existing = 0;
107 int need_messages_from_generator = 0;
108 int max_delete = 0;
109 OFF_T max_size = 0;
110 OFF_T min_size = 0;
111 int ignore_errors = 0;
112 int modify_window = 0;
113 int blocking_io = -1;
114 int checksum_seed = 0;
115 int inplace = 0;
116 int delay_updates = 0;
117 long block_size = 0; /* "long" because popt can't set an int32. */
118
119
120 /** Network address family. **/
121 #ifdef INET6
122 int default_af_hint = 0;        /* Any protocol */
123 #else
124 int default_af_hint = AF_INET;  /* Must use IPv4 */
125 #endif
126
127 /** Do not go into the background when run as --daemon.  Good
128  * for debugging and required for running as a service on W32,
129  * or under Unix process-monitors. **/
130 int no_detach
131 #if defined _WIN32 || defined __WIN32__
132         = 1;
133 #else
134         = 0;
135 #endif
136
137 int write_batch = 0;
138 int read_batch = 0;
139 int backup_dir_len = 0;
140 int backup_suffix_len;
141 unsigned int backup_dir_remainder;
142
143 char *backup_suffix = NULL;
144 char *tmpdir = NULL;
145 char *partial_dir = NULL;
146 char *basis_dir[MAX_BASIS_DIRS+1];
147 char *config_file = NULL;
148 char *shell_cmd = NULL;
149 char *log_format = NULL;
150 char *password_file = NULL;
151 char *rsync_path = RSYNC_PATH;
152 char *backup_dir = NULL;
153 char backup_dir_buf[MAXPATHLEN];
154 char *sockopts = NULL;
155 int rsync_port = 0;
156 int compare_dest = 0;
157 int copy_dest = 0;
158 int link_dest = 0;
159 int basis_dir_cnt = 0;
160 char *dest_option = NULL;
161
162 int verbose = 0;
163 int quiet = 0;
164 int log_before_transfer = 0;
165 int log_format_has_i = 0;
166 int log_format_has_o_or_i = 0;
167 int always_checksum = 0;
168 int list_only = 0;
169
170 #define MAX_BATCH_NAME_LEN 256  /* Must be less than MAXPATHLEN-13 */
171 char *batch_name = NULL;
172
173 struct chmod_mode_struct *chmod_modes = NULL;
174
175 static int daemon_opt;   /* sets am_daemon after option error-reporting */
176 static int F_option_cnt = 0;
177 static int modify_window_set;
178 static int itemize_changes = 0;
179 static int refused_delete, refused_archive_part, refused_compress;
180 static int refused_partial, refused_progress, refused_delete_before;
181 static int refused_inplace;
182 static char *max_size_arg, *min_size_arg;
183 static char tmp_partialdir[] = ".~tmp~";
184
185 /** Local address to bind.  As a character string because it's
186  * interpreted by the IPv6 layer: should be a numeric IP4 or IP6
187  * address, or a hostname. **/
188 char *bind_address;
189
190
191 static void print_rsync_version(enum logcode f)
192 {
193         char const *got_socketpair = "no ";
194         char const *have_inplace = "no ";
195         char const *hardlinks = "no ";
196         char const *links = "no ";
197         char const *ipv6 = "no ";
198         STRUCT_STAT *dumstat;
199
200 #ifdef HAVE_SOCKETPAIR
201         got_socketpair = "";
202 #endif
203
204 #ifdef HAVE_FTRUNCATE
205         have_inplace = "";
206 #endif
207
208 #ifdef SUPPORT_HARD_LINKS
209         hardlinks = "";
210 #endif
211
212 #ifdef SUPPORT_LINKS
213         links = "";
214 #endif
215
216 #ifdef INET6
217         ipv6 = "";
218 #endif
219
220         rprintf(f, "%s  version %s  protocol version %d\n",
221                 RSYNC_NAME, RSYNC_VERSION, PROTOCOL_VERSION);
222         rprintf(f, "Copyright (C) 1996-2006 by Andrew Tridgell, Wayne Davison, and others.\n");
223         rprintf(f, "<http://rsync.samba.org/>\n");
224         rprintf(f, "Capabilities: %d-bit files, %ssocketpairs, "
225                 "%shard links, %ssymlinks, batchfiles,\n",
226                 (int) (sizeof (OFF_T) * 8),
227                 got_socketpair, hardlinks, links);
228
229         /* Note that this field may not have type ino_t.  It depends
230          * on the complicated interaction between largefile feature
231          * macros. */
232         rprintf(f, "              %sinplace, %sIPv6, "
233                 "%d-bit system inums, %d-bit internal inums\n",
234                 have_inplace, ipv6,
235                 (int) (sizeof dumstat->st_ino * 8),
236                 (int) (sizeof (int64) * 8));
237 #ifdef MAINTAINER_MODE
238         rprintf(f, "Panic Action: \"%s\"\n", get_panic_action());
239 #endif
240
241 #if SIZEOF_INT64 < 8
242         rprintf(f, "WARNING: no 64-bit integers on this platform!\n");
243 #endif
244         if (sizeof (int64) != SIZEOF_INT64) {
245                 rprintf(f,
246                         "WARNING: size mismatch in SIZEOF_INT64 define (%d != %d)\n",
247                         (int) SIZEOF_INT64, (int) sizeof (int64));
248         }
249
250         rprintf(f,"\nrsync comes with ABSOLUTELY NO WARRANTY.  This is free software, and you\n");
251         rprintf(f,"are welcome to redistribute it under certain conditions.  See the GNU\n");
252         rprintf(f,"General Public Licence for details.\n");
253 }
254
255
256 void usage(enum logcode F)
257 {
258   print_rsync_version(F);
259
260   rprintf(F,"\nrsync is a file transfer program capable of efficient remote update\n");
261   rprintf(F,"via a fast differencing algorithm.\n");
262
263   rprintf(F,"\nUsage: rsync [OPTION]... SRC [SRC]... DEST\n");
264   rprintf(F,"  or   rsync [OPTION]... SRC [SRC]... [USER@]HOST:DEST\n");
265   rprintf(F,"  or   rsync [OPTION]... SRC [SRC]... [USER@]HOST::DEST\n");
266   rprintf(F,"  or   rsync [OPTION]... SRC [SRC]... rsync://[USER@]HOST[:PORT]/DEST\n");
267   rprintf(F,"  or   rsync [OPTION]... [USER@]HOST:SRC [DEST]\n");
268   rprintf(F,"  or   rsync [OPTION]... [USER@]HOST::SRC [DEST]\n");
269   rprintf(F,"  or   rsync [OPTION]... rsync://[USER@]HOST[:PORT]/SRC [DEST]\n");
270   rprintf(F,"The ':' usages connect via remote shell, while '::' & 'rsync://' usages connect\n");
271   rprintf(F,"to an rsync daemon, and require SRC or DEST to start with a module name.\n");
272   rprintf(F,"\nOptions\n");
273   rprintf(F," -v, --verbose               increase verbosity\n");
274   rprintf(F," -q, --quiet                 suppress non-error messages\n");
275   rprintf(F," -c, --checksum              skip based on checksum, not mod-time & size\n");
276   rprintf(F," -a, --archive               archive mode; same as -rlptgoD (no -H)\n");
277   rprintf(F,"     --no-OPTION             turn off an implied OPTION (e.g. --no-D)\n");
278   rprintf(F," -r, --recursive             recurse into directories\n");
279   rprintf(F," -R, --relative              use relative path names\n");
280   rprintf(F,"     --no-implied-dirs       don't send implied dirs with --relative\n");
281   rprintf(F," -b, --backup                make backups (see --suffix & --backup-dir)\n");
282   rprintf(F,"     --backup-dir=DIR        make backups into hierarchy based in DIR\n");
283   rprintf(F,"     --suffix=SUFFIX         set backup suffix (default %s w/o --backup-dir)\n",BACKUP_SUFFIX);
284   rprintf(F," -u, --update                skip files that are newer on the receiver\n");
285   rprintf(F,"     --inplace               update destination files in-place (SEE MAN PAGE)\n");
286   rprintf(F,"     --append                append data onto shorter files\n");
287   rprintf(F," -d, --dirs                  transfer directories without recursing\n");
288   rprintf(F," -l, --links                 copy symlinks as symlinks\n");
289   rprintf(F," -L, --copy-links            transform symlink into referent file/dir\n");
290   rprintf(F,"     --copy-unsafe-links     only \"unsafe\" symlinks are transformed\n");
291   rprintf(F,"     --safe-links            ignore symlinks that point outside the source tree\n");
292   rprintf(F," -H, --hard-links            preserve hard links\n");
293   rprintf(F," -K, --keep-dirlinks         treat symlinked dir on receiver as dir\n");
294   rprintf(F," -p, --perms                 preserve permissions\n");
295   rprintf(F," -E, --executability         preserve the file's executability\n");
296   rprintf(F,"     --chmod=CHMOD           change destination permissions\n");
297   rprintf(F," -o, --owner                 preserve owner (super-user only)\n");
298   rprintf(F," -g, --group                 preserve group\n");
299   rprintf(F,"     --devices               preserve device files (super-user only)\n");
300   rprintf(F,"     --specials              preserve special files\n");
301   rprintf(F," -D                          same as --devices --specials\n");
302   rprintf(F," -t, --times                 preserve times\n");
303   rprintf(F," -O, --omit-dir-times        omit directories when preserving times\n");
304   rprintf(F,"     --super                 receiver attempts super-user activities\n");
305   rprintf(F," -S, --sparse                handle sparse files efficiently\n");
306   rprintf(F," -n, --dry-run               show what would have been transferred\n");
307   rprintf(F," -W, --whole-file            copy files whole (without rsync algorithm)\n");
308   rprintf(F," -x, --one-file-system       don't cross filesystem boundaries\n");
309   rprintf(F," -B, --block-size=SIZE       force a fixed checksum block-size\n");
310   rprintf(F," -e, --rsh=COMMAND           specify the remote shell to use\n");
311   rprintf(F,"     --rsync-path=PROGRAM    specify the rsync to run on the remote machine\n");
312   rprintf(F,"     --existing              ignore non-existing files on receiving side\n");
313   rprintf(F,"     --ignore-existing       ignore files that already exist on receiving side\n");
314   rprintf(F,"     --remove-sent-files     sent files/symlinks are removed from sending side\n");
315   rprintf(F,"     --del                   an alias for --delete-during\n");
316   rprintf(F,"     --delete                delete files that don't exist on the sending side\n");
317   rprintf(F,"     --delete-before         receiver deletes before transfer (default)\n");
318   rprintf(F,"     --delete-during         receiver deletes during transfer, not before\n");
319   rprintf(F,"     --delete-after          receiver deletes after transfer, not before\n");
320   rprintf(F,"     --delete-excluded       also delete excluded files on the receiving side\n");
321   rprintf(F,"     --ignore-errors         delete even if there are I/O errors\n");
322   rprintf(F,"     --force                 force deletion of directories even if not empty\n");
323   rprintf(F,"     --max-delete=NUM        don't delete more than NUM files\n");
324   rprintf(F,"     --max-size=SIZE         don't transfer any file larger than SIZE\n");
325   rprintf(F,"     --min-size=SIZE         don't transfer any file smaller than SIZE\n");
326   rprintf(F,"     --partial               keep partially transferred files\n");
327   rprintf(F,"     --partial-dir=DIR       put a partially transferred file into DIR\n");
328   rprintf(F,"     --delay-updates         put all updated files into place at transfer's end\n");
329   rprintf(F," -m, --prune-empty-dirs      prune empty directory chains from the file-list\n");
330   rprintf(F,"     --numeric-ids           don't map uid/gid values by user/group name\n");
331   rprintf(F,"     --timeout=TIME          set I/O timeout in seconds\n");
332   rprintf(F," -I, --ignore-times          don't skip files that match in size and mod-time\n");
333   rprintf(F,"     --size-only             skip files that match in size\n");
334   rprintf(F,"     --modify-window=NUM     compare mod-times with reduced accuracy\n");
335   rprintf(F," -T, --temp-dir=DIR          create temporary files in directory DIR\n");
336   rprintf(F," -y, --fuzzy                 find similar file for basis if no dest file\n");
337   rprintf(F,"     --compare-dest=DIR      also compare destination files relative to DIR\n");
338   rprintf(F,"     --copy-dest=DIR         ... and include copies of unchanged files\n");
339   rprintf(F,"     --link-dest=DIR         hardlink to files in DIR when unchanged\n");
340   rprintf(F," -z, --compress              compress file data during the transfer\n");
341   rprintf(F,"     --compress-level=NUM    explicitly set compression level\n");
342   rprintf(F," -C, --cvs-exclude           auto-ignore files the same way CVS does\n");
343   rprintf(F," -f, --filter=RULE           add a file-filtering RULE\n");
344   rprintf(F," -F                          same as --filter='dir-merge /.rsync-filter'\n");
345   rprintf(F,"                             repeated: --filter='- .rsync-filter'\n");
346   rprintf(F,"     --exclude=PATTERN       exclude files matching PATTERN\n");
347   rprintf(F,"     --exclude-from=FILE     read exclude patterns from FILE\n");
348   rprintf(F,"     --include=PATTERN       don't exclude files matching PATTERN\n");
349   rprintf(F,"     --include-from=FILE     read include patterns from FILE\n");
350   rprintf(F,"     --files-from=FILE       read list of source-file names from FILE\n");
351   rprintf(F," -0, --from0                 all *-from/filter files are delimited by 0s\n");
352   rprintf(F,"     --address=ADDRESS       bind address for outgoing socket to daemon\n");
353   rprintf(F,"     --port=PORT             specify double-colon alternate port number\n");
354   rprintf(F,"     --sockopts=OPTIONS      specify custom TCP options\n");
355   rprintf(F,"     --blocking-io           use blocking I/O for the remote shell\n");
356   rprintf(F,"     --stats                 give some file-transfer stats\n");
357   rprintf(F," -h, --human-readable        output numbers in a human-readable format\n");
358   rprintf(F,"     --si                    like human-readable, but use powers of 1000\n");
359   rprintf(F,"     --progress              show progress during transfer\n");
360   rprintf(F," -P                          same as --partial --progress\n");
361   rprintf(F," -i, --itemize-changes       output a change-summary for all updates\n");
362   rprintf(F,"     --log-format=FORMAT     output filenames using the specified format\n");
363   rprintf(F,"     --password-file=FILE    read password from FILE\n");
364   rprintf(F,"     --list-only             list the files instead of copying them\n");
365   rprintf(F,"     --bwlimit=KBPS          limit I/O bandwidth; KBytes per second\n");
366   rprintf(F,"     --write-batch=FILE      write a batched update to FILE\n");
367   rprintf(F,"     --only-write-batch=FILE like --write-batch but w/o updating destination\n");
368   rprintf(F,"     --read-batch=FILE       read a batched update from FILE\n");
369   rprintf(F,"     --protocol=NUM          force an older protocol version to be used\n");
370 #ifdef INET6
371   rprintf(F," -4, --ipv4                  prefer IPv4\n");
372   rprintf(F," -6, --ipv6                  prefer IPv6\n");
373 #endif
374   rprintf(F,"     --version               print version number\n");
375   rprintf(F,"     --help                  show this help screen\n");
376
377   rprintf(F,"\nUse \"rsync --daemon --help\" to see the daemon-mode command-line options.\n");
378   rprintf(F,"Please see the rsync(1) and rsyncd.conf(5) man pages for full documentation.\n");
379   rprintf(F,"See http://rsync.samba.org/ for updates, bug reports, and answers\n");
380 }
381
382 enum {OPT_VERSION = 1000, OPT_DAEMON, OPT_SENDER, OPT_EXCLUDE, OPT_EXCLUDE_FROM,
383       OPT_FILTER, OPT_COMPARE_DEST, OPT_COPY_DEST, OPT_LINK_DEST, OPT_HELP,
384       OPT_INCLUDE, OPT_INCLUDE_FROM, OPT_MODIFY_WINDOW, OPT_MIN_SIZE, OPT_CHMOD,
385       OPT_READ_BATCH, OPT_WRITE_BATCH, OPT_ONLY_WRITE_BATCH, OPT_MAX_SIZE,
386       OPT_NO_D,
387       OPT_SERVER, OPT_REFUSED_BASE = 9000};
388
389 static struct poptOption long_options[] = {
390   /* longName, shortName, argInfo, argPtr, value, descrip, argDesc */
391   {"help",             0,  POPT_ARG_NONE,   0, OPT_HELP, 0, 0 },
392   {"version",          0,  POPT_ARG_NONE,   0, OPT_VERSION, 0, 0},
393   {"verbose",         'v', POPT_ARG_NONE,   0, 'v', 0, 0 },
394   {"no-verbose",       0,  POPT_ARG_VAL,    &verbose, 0, 0, 0 },
395   {"no-v",             0,  POPT_ARG_VAL,    &verbose, 0, 0, 0 },
396   {"quiet",           'q', POPT_ARG_NONE,   0, 'q', 0, 0 },
397   {"stats",            0,  POPT_ARG_NONE,   &do_stats, 0, 0, 0 },
398   {"human-readable",  'h', POPT_ARG_VAL,    &human_readable, 1, 0, 0},
399   {"si",               0,  POPT_ARG_VAL,    &human_readable, 2, 0, 0},
400   {"dry-run",         'n', POPT_ARG_NONE,   &dry_run, 0, 0, 0 },
401   {"archive",         'a', POPT_ARG_NONE,   0, 'a', 0, 0 },
402   {"recursive",       'r', POPT_ARG_VAL,    &recurse, 2, 0, 0 },
403   {"no-recursive",     0,  POPT_ARG_VAL,    &recurse, 0, 0, 0 },
404   {"no-r",             0,  POPT_ARG_VAL,    &recurse, 0, 0, 0 },
405   {"dirs",            'd', POPT_ARG_VAL,    &xfer_dirs, 2, 0, 0 },
406   {"no-dirs",          0,  POPT_ARG_VAL,    &xfer_dirs, 0, 0, 0 },
407   {"no-d",             0,  POPT_ARG_VAL,    &xfer_dirs, 0, 0, 0 },
408   {"perms",           'p', POPT_ARG_VAL,    &preserve_perms, 1, 0, 0 },
409   {"no-perms",         0,  POPT_ARG_VAL,    &preserve_perms, 0, 0, 0 },
410   {"no-p",             0,  POPT_ARG_VAL,    &preserve_perms, 0, 0, 0 },
411   {"executability",   'E', POPT_ARG_NONE,   &preserve_executability, 0, 0, 0 },
412   {"times",           't', POPT_ARG_VAL,    &preserve_times, 1, 0, 0 },
413   {"no-times",         0,  POPT_ARG_VAL,    &preserve_times, 0, 0, 0 },
414   {"no-t",             0,  POPT_ARG_VAL,    &preserve_times, 0, 0, 0 },
415   {"omit-dir-times",  'O', POPT_ARG_VAL,    &omit_dir_times, 2, 0, 0 },
416   {"modify-window",    0,  POPT_ARG_INT,    &modify_window, OPT_MODIFY_WINDOW, 0, 0 },
417   {"super",            0,  POPT_ARG_VAL,    &am_root, 2, 0, 0 },
418   {"no-super",         0,  POPT_ARG_VAL,    &am_root, 0, 0, 0 },
419   {"owner",           'o', POPT_ARG_VAL,    &preserve_uid, 1, 0, 0 },
420   {"no-owner",         0,  POPT_ARG_VAL,    &preserve_uid, 0, 0, 0 },
421   {"no-o",             0,  POPT_ARG_VAL,    &preserve_uid, 0, 0, 0 },
422   {"group",           'g', POPT_ARG_VAL,    &preserve_gid, 1, 0, 0 },
423   {"no-group",         0,  POPT_ARG_VAL,    &preserve_gid, 0, 0, 0 },
424   {"no-g",             0,  POPT_ARG_VAL,    &preserve_gid, 0, 0, 0 },
425   {0,                 'D', POPT_ARG_NONE,   0, 'D', 0, 0 },
426   {"no-D",             0,  POPT_ARG_NONE,   0, OPT_NO_D, 0, 0 },
427   {"devices",          0,  POPT_ARG_VAL,    &preserve_devices, 1, 0, 0 },
428   {"no-devices",       0,  POPT_ARG_VAL,    &preserve_devices, 0, 0, 0 },
429   {"specials",         0,  POPT_ARG_VAL,    &preserve_specials, 1, 0, 0 },
430   {"no-specials",      0,  POPT_ARG_VAL,    &preserve_specials, 0, 0, 0 },
431   {"links",           'l', POPT_ARG_VAL,    &preserve_links, 1, 0, 0 },
432   {"no-links",         0,  POPT_ARG_VAL,    &preserve_links, 0, 0, 0 },
433   {"no-l",             0,  POPT_ARG_VAL,    &preserve_links, 0, 0, 0 },
434   {"copy-links",      'L', POPT_ARG_NONE,   &copy_links, 0, 0, 0 },
435   {"copy-unsafe-links",0,  POPT_ARG_NONE,   &copy_unsafe_links, 0, 0, 0 },
436   {"safe-links",       0,  POPT_ARG_NONE,   &safe_symlinks, 0, 0, 0 },
437   {"keep-dirlinks",   'K', POPT_ARG_NONE,   &keep_dirlinks, 0, 0, 0 },
438   {"hard-links",      'H', POPT_ARG_NONE,   &preserve_hard_links, 0, 0, 0 },
439   {"relative",        'R', POPT_ARG_VAL,    &relative_paths, 1, 0, 0 },
440   {"no-relative",      0,  POPT_ARG_VAL,    &relative_paths, 0, 0, 0 },
441   {"no-R",             0,  POPT_ARG_VAL,    &relative_paths, 0, 0, 0 },
442   {"no-implied-dirs",  0,  POPT_ARG_VAL,    &implied_dirs, 0, 0, 0 },
443   {"chmod",            0,  POPT_ARG_STRING, 0, OPT_CHMOD, 0, 0 },
444   {"ignore-times",    'I', POPT_ARG_NONE,   &ignore_times, 0, 0, 0 },
445   {"size-only",        0,  POPT_ARG_NONE,   &size_only, 0, 0, 0 },
446   {"one-file-system", 'x', POPT_ARG_NONE,   0, 'x', 0, 0 },
447   {"update",          'u', POPT_ARG_NONE,   &update_only, 0, 0, 0 },
448   {"existing",         0,  POPT_ARG_NONE,   &ignore_non_existing, 0, 0, 0 },
449   {"ignore-non-existing",0,POPT_ARG_NONE,   &ignore_non_existing, 0, 0, 0 },
450   {"ignore-existing",  0,  POPT_ARG_NONE,   &ignore_existing, 0, 0, 0 },
451   {"max-size",         0,  POPT_ARG_STRING, &max_size_arg, OPT_MAX_SIZE, 0, 0 },
452   {"min-size",         0,  POPT_ARG_STRING, &min_size_arg, OPT_MIN_SIZE, 0, 0 },
453   {"sparse",          'S', POPT_ARG_NONE,   &sparse_files, 0, 0, 0 },
454   {"inplace",          0,  POPT_ARG_NONE,   &inplace, 0, 0, 0 },
455   {"append",           0,  POPT_ARG_VAL,    &append_mode, 1, 0, 0 },
456   {"del",              0,  POPT_ARG_NONE,   &delete_during, 0, 0, 0 },
457   {"delete",           0,  POPT_ARG_NONE,   &delete_mode, 0, 0, 0 },
458   {"delete-before",    0,  POPT_ARG_VAL,    &delete_before, 2, 0, 0 },
459   {"delete-during",    0,  POPT_ARG_NONE,   &delete_during, 0, 0, 0 },
460   {"delete-after",     0,  POPT_ARG_NONE,   &delete_after, 0, 0, 0 },
461   {"delete-excluded",  0,  POPT_ARG_NONE,   &delete_excluded, 0, 0, 0 },
462   {"remove-sent-files",0,  POPT_ARG_NONE,   &remove_sent_files, 0, 0, 0 },
463   {"force",            0,  POPT_ARG_NONE,   &force_delete, 0, 0, 0 },
464   {"ignore-errors",    0,  POPT_ARG_NONE,   &ignore_errors, 0, 0, 0 },
465   {"max-delete",       0,  POPT_ARG_INT,    &max_delete, 0, 0, 0 },
466   {0,                 'F', POPT_ARG_NONE,   0, 'F', 0, 0 },
467   {"filter",          'f', POPT_ARG_STRING, 0, OPT_FILTER, 0, 0 },
468   {"exclude",          0,  POPT_ARG_STRING, 0, OPT_EXCLUDE, 0, 0 },
469   {"include",          0,  POPT_ARG_STRING, 0, OPT_INCLUDE, 0, 0 },
470   {"exclude-from",     0,  POPT_ARG_STRING, 0, OPT_EXCLUDE_FROM, 0, 0 },
471   {"include-from",     0,  POPT_ARG_STRING, 0, OPT_INCLUDE_FROM, 0, 0 },
472   {"cvs-exclude",     'C', POPT_ARG_NONE,   &cvs_exclude, 0, 0, 0 },
473   {"whole-file",      'W', POPT_ARG_VAL,    &whole_file, 1, 0, 0 },
474   {"no-whole-file",    0,  POPT_ARG_VAL,    &whole_file, 0, 0, 0 },
475   {"no-W",             0,  POPT_ARG_VAL,    &whole_file, 0, 0, 0 },
476   {"checksum",        'c', POPT_ARG_NONE,   &always_checksum, 0, 0, 0 },
477   {"block-size",      'B', POPT_ARG_LONG,   &block_size, 0, 0, 0 },
478   {"compare-dest",     0,  POPT_ARG_STRING, 0, OPT_COMPARE_DEST, 0, 0 },
479   {"copy-dest",        0,  POPT_ARG_STRING, 0, OPT_COPY_DEST, 0, 0 },
480   {"link-dest",        0,  POPT_ARG_STRING, 0, OPT_LINK_DEST, 0, 0 },
481   {"fuzzy",           'y', POPT_ARG_NONE,   &fuzzy_basis, 0, 0, 0 },
482   {"compress",        'z', POPT_ARG_NONE,   0, 'z', 0, 0 },
483   {"compress-level",   0,  POPT_ARG_INT,    &def_compress_level, 'z', 0, 0 },
484   {0,                 'P', POPT_ARG_NONE,   0, 'P', 0, 0 },
485   {"progress",         0,  POPT_ARG_VAL,    &do_progress, 1, 0, 0 },
486   {"no-progress",      0,  POPT_ARG_VAL,    &do_progress, 0, 0, 0 },
487   {"partial",          0,  POPT_ARG_VAL,    &keep_partial, 1, 0, 0 },
488   {"no-partial",       0,  POPT_ARG_VAL,    &keep_partial, 0, 0, 0 },
489   {"partial-dir",      0,  POPT_ARG_STRING, &partial_dir, 0, 0, 0 },
490   {"delay-updates",    0,  POPT_ARG_NONE,   &delay_updates, 0, 0, 0 },
491   {"prune-empty-dirs",'m', POPT_ARG_NONE,   &prune_empty_dirs, 0, 0, 0 },
492   {"log-format",       0,  POPT_ARG_STRING, &log_format, 0, 0, 0 },
493   {"itemize-changes", 'i', POPT_ARG_NONE,   0, 'i', 0, 0 },
494   {"bwlimit",          0,  POPT_ARG_INT,    &bwlimit, 0, 0, 0 },
495   {"backup",          'b', POPT_ARG_NONE,   &make_backups, 0, 0, 0 },
496   {"backup-dir",       0,  POPT_ARG_STRING, &backup_dir, 0, 0, 0 },
497   {"suffix",           0,  POPT_ARG_STRING, &backup_suffix, 0, 0, 0 },
498   {"list-only",        0,  POPT_ARG_VAL,    &list_only, 2, 0, 0 },
499   {"read-batch",       0,  POPT_ARG_STRING, &batch_name, OPT_READ_BATCH, 0, 0 },
500   {"write-batch",      0,  POPT_ARG_STRING, &batch_name, OPT_WRITE_BATCH, 0, 0 },
501   {"only-write-batch", 0,  POPT_ARG_STRING, &batch_name, OPT_ONLY_WRITE_BATCH, 0, 0 },
502   {"files-from",       0,  POPT_ARG_STRING, &files_from, 0, 0, 0 },
503   {"from0",           '0', POPT_ARG_NONE,   &eol_nulls, 0, 0, 0},
504   {"numeric-ids",      0,  POPT_ARG_NONE,   &numeric_ids, 0, 0, 0 },
505   {"timeout",          0,  POPT_ARG_INT,    &io_timeout, 0, 0, 0 },
506   {"rsh",             'e', POPT_ARG_STRING, &shell_cmd, 0, 0, 0 },
507   {"rsync-path",       0,  POPT_ARG_STRING, &rsync_path, 0, 0, 0 },
508   {"temp-dir",        'T', POPT_ARG_STRING, &tmpdir, 0, 0, 0 },
509 #ifdef INET6
510   {"ipv4",            '4', POPT_ARG_VAL,    &default_af_hint, AF_INET, 0, 0 },
511   {"ipv6",            '6', POPT_ARG_VAL,    &default_af_hint, AF_INET6, 0, 0 },
512 #endif
513   {"address",          0,  POPT_ARG_STRING, &bind_address, 0, 0, 0 },
514   {"port",             0,  POPT_ARG_INT,    &rsync_port, 0, 0, 0 },
515   {"sockopts",         0,  POPT_ARG_STRING, &sockopts, 0, 0, 0 },
516   {"password-file",    0,  POPT_ARG_STRING, &password_file, 0, 0, 0 },
517   {"blocking-io",      0,  POPT_ARG_VAL,    &blocking_io, 1, 0, 0 },
518   {"no-blocking-io",   0,  POPT_ARG_VAL,    &blocking_io, 0, 0, 0 },
519   {"protocol",         0,  POPT_ARG_INT,    &protocol_version, 0, 0, 0 },
520   {"checksum-seed",    0,  POPT_ARG_INT,    &checksum_seed, 0, 0, 0 },
521   {"server",           0,  POPT_ARG_NONE,   0, OPT_SERVER, 0, 0 },
522   {"sender",           0,  POPT_ARG_NONE,   0, OPT_SENDER, 0, 0 },
523   /* All the following options switch us into daemon-mode option-parsing. */
524   {"config",           0,  POPT_ARG_STRING, 0, OPT_DAEMON, 0, 0 },
525   {"daemon",           0,  POPT_ARG_NONE,   0, OPT_DAEMON, 0, 0 },
526   {"detach",           0,  POPT_ARG_NONE,   0, OPT_DAEMON, 0, 0 },
527   {"no-detach",        0,  POPT_ARG_NONE,   0, OPT_DAEMON, 0, 0 },
528   {0,0,0,0, 0, 0, 0}
529 };
530
531 static void daemon_usage(enum logcode F)
532 {
533   print_rsync_version(F);
534
535   rprintf(F,"\nUsage: rsync --daemon [OPTION]...\n");
536   rprintf(F,"     --address=ADDRESS       bind to the specified address\n");
537   rprintf(F,"     --bwlimit=KBPS          limit I/O bandwidth; KBytes per second\n");
538   rprintf(F,"     --config=FILE           specify alternate rsyncd.conf file\n");
539   rprintf(F,"     --no-detach             do not detach from the parent\n");
540   rprintf(F,"     --port=PORT             listen on alternate port number\n");
541   rprintf(F,"     --sockopts=OPTIONS      specify custom TCP options\n");
542   rprintf(F," -v, --verbose               increase verbosity\n");
543 #ifdef INET6
544   rprintf(F," -4, --ipv4                  prefer IPv4\n");
545   rprintf(F," -6, --ipv6                  prefer IPv6\n");
546 #endif
547   rprintf(F,"     --help                  show this help screen\n");
548
549   rprintf(F,"\nIf you were not trying to invoke rsync as a daemon, avoid using any of the\n");
550   rprintf(F,"daemon-specific rsync options.  See also the rsyncd.conf(5) man page.\n");
551 }
552
553 static struct poptOption long_daemon_options[] = {
554   /* longName, shortName, argInfo, argPtr, value, descrip, argDesc */
555   {"address",          0,  POPT_ARG_STRING, &bind_address, 0, 0, 0 },
556   {"bwlimit",          0,  POPT_ARG_INT,    &daemon_bwlimit, 0, 0, 0 },
557   {"config",           0,  POPT_ARG_STRING, &config_file, 0, 0, 0 },
558   {"daemon",           0,  POPT_ARG_NONE,   &daemon_opt, 0, 0, 0 },
559 #ifdef INET6
560   {"ipv4",            '4', POPT_ARG_VAL,    &default_af_hint, AF_INET, 0, 0 },
561   {"ipv6",            '6', POPT_ARG_VAL,    &default_af_hint, AF_INET6, 0, 0 },
562 #endif
563   {"detach",           0,  POPT_ARG_VAL,    &no_detach, 0, 0, 0 },
564   {"no-detach",        0,  POPT_ARG_VAL,    &no_detach, 1, 0, 0 },
565   {"port",             0,  POPT_ARG_INT,    &rsync_port, 0, 0, 0 },
566   {"sockopts",         0,  POPT_ARG_STRING, &sockopts, 0, 0, 0 },
567   {"protocol",         0,  POPT_ARG_INT,    &protocol_version, 0, 0, 0 },
568   {"server",           0,  POPT_ARG_NONE,   &am_server, 0, 0, 0 },
569   {"temp-dir",        'T', POPT_ARG_STRING, &tmpdir, 0, 0, 0 },
570   {"verbose",         'v', POPT_ARG_NONE,   0, 'v', 0, 0 },
571   {"no-verbose",       0,  POPT_ARG_VAL,    &verbose, 0, 0, 0 },
572   {"no-v",             0,  POPT_ARG_VAL,    &verbose, 0, 0, 0 },
573   {"help",            'h', POPT_ARG_NONE,   0, 'h', 0, 0 },
574   {0,0,0,0, 0, 0, 0}
575 };
576
577
578 static char err_buf[200];
579
580
581 /**
582  * Store the option error message, if any, so that we can log the
583  * connection attempt (which requires parsing the options), and then
584  * show the error later on.
585  **/
586 void option_error(void)
587 {
588         if (!err_buf[0]) {
589                 strcpy(err_buf, "Error parsing options: "
590                     "option may be supported on client but not on server?\n");
591         }
592
593         rprintf(FERROR, RSYNC_NAME ": %s", err_buf);
594 }
595
596
597 /**
598  * Tweak the option table to disable all options that the rsyncd.conf
599  * file has told us to refuse.
600  **/
601 static void set_refuse_options(char *bp)
602 {
603         struct poptOption *op;
604         char *cp, shortname[2];
605         int is_wild, found_match;
606
607         shortname[1] = '\0';
608
609         while (1) {
610                 while (*bp == ' ') bp++;
611                 if (!*bp)
612                         break;
613                 if ((cp = strchr(bp, ' ')) != NULL)
614                         *cp= '\0';
615                 is_wild = strpbrk(bp, "*?[") != NULL;
616                 found_match = 0;
617                 for (op = long_options; ; op++) {
618                         *shortname = op->shortName;
619                         if (!op->longName && !*shortname)
620                                 break;
621                         if ((op->longName && wildmatch(bp, op->longName))
622                             || (*shortname && wildmatch(bp, shortname))) {
623                                 if (op->argInfo == POPT_ARG_VAL)
624                                         op->argInfo = POPT_ARG_NONE;
625                                 op->val = (op - long_options) + OPT_REFUSED_BASE;
626                                 found_match = 1;
627                                 /* These flags are set to let us easily check
628                                  * an implied option later in the code. */
629                                 switch (*shortname) {
630                                 case 'r': case 'd': case 'l': case 'p':
631                                 case 't': case 'g': case 'o': case 'D':
632                                         refused_archive_part = op->val;
633                                         break;
634                                 case 'z':
635                                         refused_compress = op->val;
636                                         break;
637                                 case '\0':
638                                         if (wildmatch("delete", op->longName))
639                                                 refused_delete = op->val;
640                                         else if (wildmatch("delete-before", op->longName))
641                                                 refused_delete_before = op->val;
642                                         else if (wildmatch("partial", op->longName))
643                                                 refused_partial = op->val;
644                                         else if (wildmatch("progress", op->longName))
645                                                 refused_progress = op->val;
646                                         else if (wildmatch("inplace", op->longName))
647                                                 refused_inplace = op->val;
648                                         break;
649                                 }
650                                 if (!is_wild)
651                                         break;
652                         }
653                 }
654                 if (!found_match) {
655                         rprintf(FLOG, "No match for refuse-options string \"%s\"\n",
656                                 bp);
657                 }
658                 if (!cp)
659                         break;
660                 *cp = ' ';
661                 bp = cp + 1;
662         }
663
664         for (op = long_options; ; op++) {
665                 *shortname = op->shortName;
666                 if (!op->longName && !*shortname)
667                         break;
668                 if (op->val == OPT_DAEMON) {
669                         if (op->argInfo == POPT_ARG_VAL)
670                                 op->argInfo = POPT_ARG_NONE;
671                         op->val = (op - long_options) + OPT_REFUSED_BASE;
672                 }
673         }
674 }
675
676
677 static int count_args(const char **argv)
678 {
679         int i = 0;
680
681         if (argv) {
682                 while (argv[i] != NULL)
683                         i++;
684         }
685
686         return i;
687 }
688
689
690 static OFF_T parse_size_arg(char **size_arg, char def_suf)
691 {
692         int reps, mult, make_compatible = 0;
693         const char *arg;
694         OFF_T size = 1;
695
696         for (arg = *size_arg; isdigit(*(uchar*)arg); arg++) {}
697         if (*arg == '.')
698                 for (arg++; isdigit(*(uchar*)arg); arg++) {}
699         switch (*arg && *arg != '+' && *arg != '-' ? *arg++ : def_suf) {
700         case 'b': case 'B':
701                 reps = 0;
702                 break;
703         case 'k': case 'K':
704                 reps = 1;
705                 break;
706         case 'm': case 'M':
707                 reps = 2;
708                 break;
709         case 'g': case 'G':
710                 reps = 3;
711                 break;
712         default:
713                 return -1;
714         }
715         if (*arg == 'b' || *arg == 'B')
716                 mult = 1000, make_compatible = 1, arg++;
717         else if (!*arg || *arg == '+' || *arg == '-')
718                 mult = 1024;
719         else if (strncasecmp(arg, "ib", 2) == 0)
720                 mult = 1024, arg += 2;
721         else
722                 return -1;
723         while (reps--)
724                 size *= mult;
725         size *= atof(*size_arg);
726         if ((*arg == '+' || *arg == '-') && arg[1] == '1')
727                 size += atoi(arg), make_compatible = 1, arg += 2;
728         if (*arg)
729                 return -1;
730         if (size > 0 && make_compatible) {
731                 /* We convert this manually because we may need %lld precision,
732                  * and that's not a portable sprintf() escape. */
733                 char buf[128], *s = buf + sizeof buf - 1;
734                 OFF_T num = size;
735                 *s = '\0';
736                 while (num) {
737                         *--s = (num % 10) + '0';
738                         num /= 10;
739                 }
740                 if (!(*size_arg = strdup(s)))
741                         out_of_memory("parse_size_arg");
742         }
743         return size;
744 }
745
746
747 static void create_refuse_error(int which)
748 {
749         /* The "which" value is the index + OPT_REFUSED_BASE. */
750         struct poptOption *op = &long_options[which - OPT_REFUSED_BASE];
751         int n = snprintf(err_buf, sizeof err_buf,
752                          "The server is configured to refuse --%s\n",
753                          op->longName) - 1;
754         if (op->shortName) {
755                 snprintf(err_buf + n, sizeof err_buf - n,
756                          " (-%c)\n", op->shortName);
757         }
758 }
759
760
761 /**
762  * Process command line arguments.  Called on both local and remote.
763  *
764  * @retval 1 if all options are OK; with globals set to appropriate
765  * values
766  *
767  * @retval 0 on error, with err_buf containing an explanation
768  **/
769 int parse_arguments(int *argc, const char ***argv, int frommain)
770 {
771         int opt;
772         char *ref = lp_refuse_options(module_id);
773         const char *arg;
774         poptContext pc;
775
776         if (ref && *ref)
777                 set_refuse_options(ref);
778
779         /* TODO: Call poptReadDefaultConfig; handle errors. */
780
781         /* The context leaks in case of an error, but if there's a
782          * problem we always exit anyhow. */
783         pc = poptGetContext(RSYNC_NAME, *argc, *argv, long_options, 0);
784         poptReadDefaultConfig(pc, 0);
785
786         while ((opt = poptGetNextOpt(pc)) != -1) {
787                 /* most options are handled automatically by popt;
788                  * only special cases are returned and listed here. */
789
790                 switch (opt) {
791                 case OPT_VERSION:
792                         print_rsync_version(FINFO);
793                         exit_cleanup(0);
794
795                 case OPT_SERVER:
796                         if (!am_server) {
797                                 /* Disable popt aliases on the server side and
798                                  * then start parsing the options again. */
799                                 poptFreeContext(pc);
800                                 pc = poptGetContext(RSYNC_NAME, *argc, *argv,
801                                                     long_options, 0);
802                                 am_server = 1;
803                         }
804                         break;
805
806                 case OPT_SENDER:
807                         if (!am_server) {
808                                 usage(FERROR);
809                                 exit_cleanup(RERR_SYNTAX);
810                         }
811                         am_sender = 1;
812                         break;
813
814                 case OPT_DAEMON:
815                         if (am_daemon) {
816                                 strcpy(err_buf, "Attempt to hack rsync thwarted!\n");
817                                 return 0;
818                         }
819                         poptFreeContext(pc);
820                         pc = poptGetContext(RSYNC_NAME, *argc, *argv,
821                                             long_daemon_options, 0);
822                         while ((opt = poptGetNextOpt(pc)) != -1) {
823                                 switch (opt) {
824                                 case 'h':
825                                         daemon_usage(FINFO);
826                                         exit_cleanup(0);
827
828                                 case 'v':
829                                         verbose++;
830                                         break;
831
832                                 default:
833                                         rprintf(FERROR,
834                                             "rsync: %s: %s (in daemon mode)\n",
835                                             poptBadOption(pc, POPT_BADOPTION_NOALIAS),
836                                             poptStrerror(opt));
837                                         goto daemon_error;
838                                 }
839                         }
840
841                         if (tmpdir && strlen(tmpdir) >= MAXPATHLEN - 10) {
842                                 snprintf(err_buf, sizeof err_buf,
843                                          "the --temp-dir path is WAY too long.\n");
844                                 return 0;
845                         }
846
847                         if (!daemon_opt) {
848                                 rprintf(FERROR, "Daemon option(s) used without --daemon.\n");
849                             daemon_error:
850                                 rprintf(FERROR,
851                                     "(Type \"rsync --daemon --help\" for assistance with daemon mode.)\n");
852                                 exit_cleanup(RERR_SYNTAX);
853                         }
854
855                         *argv = poptGetArgs(pc);
856                         *argc = count_args(*argv);
857                         am_starting_up = 0;
858                         daemon_opt = 0;
859                         am_daemon = 1;
860                         return 1;
861
862                 case OPT_MODIFY_WINDOW:
863                         /* The value has already been set by popt, but
864                          * we need to remember that we're using a
865                          * non-default setting. */
866                         modify_window_set = 1;
867                         break;
868
869                 case OPT_FILTER:
870                         parse_rule(&filter_list, poptGetOptArg(pc), 0, 0);
871                         break;
872
873                 case OPT_EXCLUDE:
874                         parse_rule(&filter_list, poptGetOptArg(pc),
875                                    0, XFLG_OLD_PREFIXES);
876                         break;
877
878                 case OPT_INCLUDE:
879                         parse_rule(&filter_list, poptGetOptArg(pc),
880                                    MATCHFLG_INCLUDE, XFLG_OLD_PREFIXES);
881                         break;
882
883                 case OPT_EXCLUDE_FROM:
884                 case OPT_INCLUDE_FROM:
885                         arg = poptGetOptArg(pc);
886                         if (sanitize_paths)
887                                 arg = sanitize_path(NULL, arg, NULL, 0);
888                         if (server_filter_list.head) {
889                                 char *cp = (char *)arg;
890                                 if (!*cp)
891                                         goto options_rejected;
892                                 clean_fname(cp, 1);
893                                 if (check_filter(&server_filter_list, cp, 0) < 0)
894                                         goto options_rejected;
895                         }
896                         parse_filter_file(&filter_list, arg,
897                                 opt == OPT_INCLUDE_FROM ? MATCHFLG_INCLUDE : 0,
898                                 XFLG_FATAL_ERRORS | XFLG_OLD_PREFIXES);
899                         break;
900
901                 case 'a':
902                         if (refused_archive_part) {
903                                 create_refuse_error(refused_archive_part);
904                                 return 0;
905                         }
906                         if (!recurse) /* preserve recurse == 2 */
907                                 recurse = 1;
908 #ifdef SUPPORT_LINKS
909                         preserve_links = 1;
910 #endif
911                         preserve_perms = 1;
912                         preserve_times = 1;
913                         preserve_gid = 1;
914                         preserve_uid = 1;
915                         preserve_devices = 1;
916                         preserve_specials = 1;
917                         break;
918
919                 case 'D':
920                         preserve_devices = preserve_specials = 1;
921                         break;
922
923                 case OPT_NO_D:
924                         preserve_devices = preserve_specials = 0;
925                         break;
926
927                 case 'i':
928                         itemize_changes++;
929                         break;
930
931                 case 'v':
932                         verbose++;
933                         break;
934
935                 case 'q':
936                         if (frommain)
937                                 quiet++;
938                         break;
939
940                 case 'x':
941                         one_file_system++;
942                         break;
943
944                 case 'F':
945                         switch (++F_option_cnt) {
946                         case 1:
947                                 parse_rule(&filter_list,": /.rsync-filter",0,0);
948                                 break;
949                         case 2:
950                                 parse_rule(&filter_list,"- .rsync-filter",0,0);
951                                 break;
952                         }
953                         break;
954
955                 case 'P':
956                         if (refused_partial || refused_progress) {
957                                 create_refuse_error(refused_partial
958                                     ? refused_partial : refused_progress);
959                                 return 0;
960                         }
961                         do_progress = 1;
962                         keep_partial = 1;
963                         break;
964
965                 case 'z':
966                         if (def_compress_level < Z_DEFAULT_COMPRESSION
967                          || def_compress_level > Z_BEST_COMPRESSION) {
968                                 snprintf(err_buf, sizeof err_buf,
969                                         "--compress-level value is invalid: %d\n",
970                                         def_compress_level);
971                                 return 0;
972                         }
973                         do_compression = def_compress_level != Z_NO_COMPRESSION;
974                         if (do_compression && refused_compress) {
975                                 create_refuse_error(refused_compress);
976                                 return 0;
977                         }
978                         break;
979
980                 case OPT_WRITE_BATCH:
981                         /* batch_name is already set */
982                         write_batch = 1;
983                         break;
984
985                 case OPT_ONLY_WRITE_BATCH:
986                         /* batch_name is already set */
987                         write_batch = -1;
988                         break;
989
990                 case OPT_READ_BATCH:
991                         /* batch_name is already set */
992                         read_batch = 1;
993                         break;
994
995                 case OPT_MAX_SIZE:
996                         if ((max_size = parse_size_arg(&max_size_arg, 'b')) <= 0) {
997                                 snprintf(err_buf, sizeof err_buf,
998                                         "--max-size value is invalid: %s\n",
999                                         max_size_arg);
1000                                 return 0;
1001                         }
1002                         break;
1003
1004                 case OPT_MIN_SIZE:
1005                         if ((min_size = parse_size_arg(&min_size_arg, 'b')) <= 0) {
1006                                 snprintf(err_buf, sizeof err_buf,
1007                                         "--min-size value is invalid: %s\n",
1008                                         min_size_arg);
1009                                 return 0;
1010                         }
1011                         break;
1012
1013                 case OPT_LINK_DEST:
1014 #ifdef HAVE_LINK
1015                         link_dest = 1;
1016                         dest_option = "--link-dest";
1017                         goto set_dest_dir;
1018 #else
1019                         snprintf(err_buf, sizeof err_buf,
1020                                  "hard links are not supported on this %s\n",
1021                                  am_server ? "server" : "client");
1022                         return 0;
1023 #endif
1024
1025                 case OPT_COPY_DEST:
1026                         copy_dest = 1;
1027                         dest_option = "--copy-dest";
1028                         goto set_dest_dir;
1029
1030                 case OPT_COMPARE_DEST:
1031                         compare_dest = 1;
1032                         dest_option = "--compare-dest";
1033                 set_dest_dir:
1034                         if (basis_dir_cnt >= MAX_BASIS_DIRS) {
1035                                 snprintf(err_buf, sizeof err_buf,
1036                                         "ERROR: at most %d %s args may be specified\n",
1037                                         MAX_BASIS_DIRS, dest_option);
1038                                 return 0;
1039                         }
1040                         arg = poptGetOptArg(pc);
1041                         if (sanitize_paths)
1042                                 arg = sanitize_path(NULL, arg, NULL, 0);
1043                         basis_dir[basis_dir_cnt++] = (char *)arg;
1044                         break;
1045
1046                 case OPT_CHMOD:
1047                         arg = poptGetOptArg(pc);
1048                         if (!parse_chmod(arg, &chmod_modes)) {
1049                                 snprintf(err_buf, sizeof err_buf,
1050                                     "Invalid argument passed to --chmod (%s)\n",
1051                                     arg);
1052                                 return 0;
1053                         }
1054                         break;
1055
1056                 case OPT_HELP:
1057                         usage(FINFO);
1058                         exit_cleanup(0);
1059
1060                 default:
1061                         /* A large opt value means that set_refuse_options()
1062                          * turned this option off. */
1063                         if (opt >= OPT_REFUSED_BASE) {
1064                                 create_refuse_error(opt);
1065                                 return 0;
1066                         }
1067                         snprintf(err_buf, sizeof err_buf, "%s%s: %s\n",
1068                                  am_server ? "on remote machine: " : "",
1069                                  poptBadOption(pc, POPT_BADOPTION_NOALIAS),
1070                                  poptStrerror(opt));
1071                         return 0;
1072                 }
1073         }
1074
1075         if (human_readable && *argc == 2) {
1076                 usage(FINFO);
1077                 exit_cleanup(0);
1078         }
1079
1080 #ifndef SUPPORT_LINKS
1081         if (preserve_links && !am_sender) {
1082                 snprintf(err_buf, sizeof err_buf,
1083                          "symlinks are not supported on this %s\n",
1084                          am_server ? "server" : "client");
1085                 return 0;
1086         }
1087 #endif
1088
1089 #ifndef SUPPORT_HARD_LINKS
1090         if (preserve_hard_links) {
1091                 snprintf(err_buf, sizeof err_buf,
1092                          "hard links are not supported on this %s\n",
1093                          am_server ? "server" : "client");
1094                 return 0;
1095         }
1096 #endif
1097
1098         if (write_batch && read_batch) {
1099                 snprintf(err_buf, sizeof err_buf,
1100                         "--write-batch and --read-batch can not be used together\n");
1101                 return 0;
1102         }
1103         if (write_batch > 0 || read_batch) {
1104                 if (am_server) {
1105                         rprintf(FINFO,
1106                                 "ignoring --%s-batch option sent to server\n",
1107                                 write_batch ? "write" : "read");
1108                         /* We don't actually exit_cleanup(), so that we can
1109                          * still service older version clients that still send
1110                          * batch args to server. */
1111                         read_batch = write_batch = 0;
1112                         batch_name = NULL;
1113                 } else if (dry_run)
1114                         write_batch = 0;
1115         }
1116         if (read_batch && files_from) {
1117                 snprintf(err_buf, sizeof err_buf,
1118                         "--read-batch cannot be used with --files-from\n");
1119                 return 0;
1120         }
1121         if (batch_name && strlen(batch_name) > MAX_BATCH_NAME_LEN) {
1122                 snprintf(err_buf, sizeof err_buf,
1123                         "the batch-file name must be %d characters or less.\n",
1124                         MAX_BATCH_NAME_LEN);
1125                 return 0;
1126         }
1127
1128         if (tmpdir && strlen(tmpdir) >= MAXPATHLEN - 10) {
1129                 snprintf(err_buf, sizeof err_buf,
1130                          "the --temp-dir path is WAY too long.\n");
1131                 return 0;
1132         }
1133
1134         if (compare_dest + copy_dest + link_dest > 1) {
1135                 snprintf(err_buf, sizeof err_buf,
1136                         "You may not mix --compare-dest, --copy-dest, and --link-dest.\n");
1137                 return 0;
1138         }
1139
1140         if (files_from) {
1141                 if (recurse == 1) /* preserve recurse == 2 */
1142                         recurse = 0;
1143                 if (xfer_dirs < 0)
1144                         xfer_dirs = 1;
1145         }
1146
1147         if (xfer_dirs < 1)
1148                 xfer_dirs = recurse || list_only;
1149
1150         if (relative_paths < 0)
1151                 relative_paths = files_from? 1 : 0;
1152         if (!relative_paths)
1153                 implied_dirs = 0;
1154
1155         if (!!delete_before + delete_during + delete_after > 1) {
1156                 snprintf(err_buf, sizeof err_buf,
1157                         "You may not combine multiple --delete-WHEN options.\n");
1158                 return 0;
1159         }
1160         if (delete_before || delete_during || delete_after)
1161                 delete_mode = 1;
1162         else if (delete_mode || delete_excluded) {
1163                 if (refused_delete_before) {
1164                         create_refuse_error(refused_delete_before);
1165                         return 0;
1166                 }
1167                 delete_mode = delete_before = 1;
1168         }
1169         saw_delete_opt = delete_mode;
1170         saw_delete_excluded_opt = delete_excluded;
1171         if (!xfer_dirs) {
1172                 delete_before = delete_during = delete_after = 0;
1173                 delete_mode = delete_excluded = 0;
1174         }
1175
1176         if (delete_mode && refused_delete) {
1177                 create_refuse_error(refused_delete);
1178                 return 0;
1179         }
1180
1181         if (remove_sent_files) {
1182                 /* We only want to infer this refusal of --remove-sent-files
1183                  * via the refusal of "delete", not any of the "delete-FOO"
1184                  * options. */
1185                 if (refused_delete && am_sender) {
1186                         create_refuse_error(refused_delete);
1187                         return 0;
1188                 }
1189                 need_messages_from_generator = 1;
1190         }
1191
1192         *argv = poptGetArgs(pc);
1193         *argc = count_args(*argv);
1194
1195         if (sanitize_paths) {
1196                 int i;
1197                 for (i = *argc; i-- > 0; )
1198                         (*argv)[i] = sanitize_path(NULL, (*argv)[i], "", 0);
1199                 if (tmpdir)
1200                         tmpdir = sanitize_path(NULL, tmpdir, NULL, 0);
1201                 if (partial_dir)
1202                         partial_dir = sanitize_path(NULL, partial_dir, NULL, 0);
1203                 if (backup_dir)
1204                         backup_dir = sanitize_path(NULL, backup_dir, NULL, 0);
1205         }
1206         if (server_filter_list.head && !am_sender) {
1207                 struct filter_list_struct *elp = &server_filter_list;
1208                 int i;
1209                 if (tmpdir) {
1210                         if (!*tmpdir)
1211                                 goto options_rejected;
1212                         clean_fname(tmpdir, 1);
1213                         if (check_filter(elp, tmpdir, 1) < 0)
1214                                 goto options_rejected;
1215                 }
1216                 if (partial_dir && *partial_dir) {
1217                         clean_fname(partial_dir, 1);
1218                         if (check_filter(elp, partial_dir, 1) < 0)
1219                                 goto options_rejected;
1220                 }
1221                 for (i = 0; i < basis_dir_cnt; i++) {
1222                         if (!*basis_dir[i])
1223                                 goto options_rejected;
1224                         clean_fname(basis_dir[i], 1);
1225                         if (check_filter(elp, basis_dir[i], 1) < 0)
1226                                 goto options_rejected;
1227                 }
1228                 if (backup_dir) {
1229                         if (!*backup_dir)
1230                                 goto options_rejected;
1231                         clean_fname(backup_dir, 1);
1232                         if (check_filter(elp, backup_dir, 1) < 0) {
1233                             options_rejected:
1234                                 snprintf(err_buf, sizeof err_buf,
1235                                     "Your options have been rejected by the server.\n");
1236                                 return 0;
1237                         }
1238                 }
1239         }
1240
1241         if (!backup_suffix)
1242                 backup_suffix = backup_dir ? "" : BACKUP_SUFFIX;
1243         backup_suffix_len = strlen(backup_suffix);
1244         if (strchr(backup_suffix, '/') != NULL) {
1245                 snprintf(err_buf, sizeof err_buf,
1246                         "--suffix cannot contain slashes: %s\n",
1247                         backup_suffix);
1248                 return 0;
1249         }
1250         if (backup_dir) {
1251                 backup_dir_len = strlcpy(backup_dir_buf, backup_dir, sizeof backup_dir_buf);
1252                 backup_dir_remainder = sizeof backup_dir_buf - backup_dir_len;
1253                 if (backup_dir_remainder < 32) {
1254                         snprintf(err_buf, sizeof err_buf,
1255                                 "the --backup-dir path is WAY too long.\n");
1256                         return 0;
1257                 }
1258                 if (backup_dir_buf[backup_dir_len - 1] != '/') {
1259                         backup_dir_buf[backup_dir_len++] = '/';
1260                         backup_dir_buf[backup_dir_len] = '\0';
1261                 }
1262                 if (verbose > 1 && !am_sender)
1263                         rprintf(FINFO, "backup_dir is %s\n", backup_dir_buf);
1264         } else if (!backup_suffix_len && (!am_server || !am_sender)) {
1265                 snprintf(err_buf, sizeof err_buf,
1266                         "--suffix cannot be a null string without --backup-dir\n");
1267                 return 0;
1268         } else if (make_backups && delete_mode && !delete_excluded) {
1269                 snprintf(backup_dir_buf, sizeof backup_dir_buf,
1270                         "P *%s", backup_suffix);
1271                 parse_rule(&filter_list, backup_dir_buf, 0, 0);
1272         }
1273         if (make_backups && !backup_dir)
1274                 omit_dir_times = 1;
1275
1276         if (log_format) {
1277                 if (am_server && log_format_has(log_format, 'I'))
1278                         log_format_has_i = 2;
1279                 else if (log_format_has(log_format, 'i'))
1280                         log_format_has_i = itemize_changes | 1;
1281                 if (!log_format_has(log_format, 'b')
1282                  && !log_format_has(log_format, 'c'))
1283                         log_before_transfer = !am_server;
1284         } else if (itemize_changes) {
1285                 log_format = "%i %n%L";
1286                 log_format_has_i = itemize_changes;
1287                 log_before_transfer = !am_server;
1288         }
1289
1290         if ((do_progress || dry_run) && !verbose && !log_before_transfer
1291             && !am_server)
1292                 verbose = 1;
1293
1294         if (dry_run)
1295                 do_xfers = 0;
1296
1297         set_io_timeout(io_timeout);
1298
1299         if (verbose && !log_format) {
1300                 log_format = "%n%L";
1301                 log_before_transfer = !am_server;
1302         }
1303         if (log_format_has_i || log_format_has(log_format, 'o'))
1304                 log_format_has_o_or_i = 1;
1305
1306         if (daemon_bwlimit && (!bwlimit || bwlimit > daemon_bwlimit))
1307                 bwlimit = daemon_bwlimit;
1308         if (bwlimit) {
1309                 bwlimit_writemax = (size_t)bwlimit * 128;
1310                 if (bwlimit_writemax < 512)
1311                         bwlimit_writemax = 512;
1312         }
1313
1314         if (sparse_files && inplace) {
1315                 /* Note: we don't check for this below, because --append is
1316                  * OK with --sparse (as long as redos are handled right). */
1317                 snprintf(err_buf, sizeof err_buf,
1318                          "--sparse cannot be used with --inplace\n");
1319                 return 0;
1320         }
1321
1322         if (append_mode) {
1323                 if (whole_file > 0) {
1324                         snprintf(err_buf, sizeof err_buf,
1325                                  "--append cannot be used with --whole-file\n");
1326                         return 0;
1327                 }
1328                 if (refused_inplace) {
1329                         create_refuse_error(refused_inplace);
1330                         return 0;
1331                 }
1332                 inplace = 1;
1333         }
1334
1335         if (delay_updates && !partial_dir)
1336                 partial_dir = tmp_partialdir;
1337
1338         if (inplace) {
1339 #ifdef HAVE_FTRUNCATE
1340                 if (partial_dir) {
1341                         snprintf(err_buf, sizeof err_buf,
1342                                  "--%s cannot be used with --%s\n",
1343                                  append_mode ? "append" : "inplace",
1344                                  delay_updates ? "delay-updates" : "partial-dir");
1345                         return 0;
1346                 }
1347                 /* --inplace implies --partial for refusal purposes, but we
1348                  * clear the keep_partial flag for internal logic purposes. */
1349                 if (refused_partial) {
1350                         create_refuse_error(refused_partial);
1351                         return 0;
1352                 }
1353                 keep_partial = 0;
1354 #else
1355                 snprintf(err_buf, sizeof err_buf,
1356                          "--%s is not supported on this %s\n",
1357                          append_mode ? "append" : "inplace",
1358                          am_server ? "server" : "client");
1359                 return 0;
1360 #endif
1361         } else {
1362                 if (keep_partial && !partial_dir) {
1363                         if ((arg = getenv("RSYNC_PARTIAL_DIR")) != NULL && *arg)
1364                                 partial_dir = strdup(arg);
1365                 }
1366                 if (partial_dir) {
1367                         if (*partial_dir)
1368                                 clean_fname(partial_dir, 1);
1369                         if (!*partial_dir || strcmp(partial_dir, ".") == 0)
1370                                 partial_dir = NULL;
1371                         else if (*partial_dir != '/') {
1372                                 parse_rule(&filter_list, partial_dir,
1373                                     MATCHFLG_NO_PREFIXES|MATCHFLG_DIRECTORY, 0);
1374                         }
1375                         if (!partial_dir && refused_partial) {
1376                                 create_refuse_error(refused_partial);
1377                                 return 0;
1378                         }
1379                         keep_partial = 1;
1380                 }
1381         }
1382
1383         if (files_from) {
1384                 char *h, *p;
1385                 int q;
1386                 if (*argc > 2 || (!am_daemon && *argc == 1)) {
1387                         usage(FERROR);
1388                         exit_cleanup(RERR_SYNTAX);
1389                 }
1390                 if (strcmp(files_from, "-") == 0) {
1391                         filesfrom_fd = 0;
1392                         if (am_server)
1393                                 filesfrom_host = ""; /* reading from socket */
1394                 } else if ((p = check_for_hostspec(files_from, &h, &q)) != 0) {
1395                         if (am_server) {
1396                                 snprintf(err_buf, sizeof err_buf,
1397                                         "The --files-from sent to the server cannot specify a host.\n");
1398                                 return 0;
1399                         }
1400                         files_from = p;
1401                         filesfrom_host = h;
1402                         if (strcmp(files_from, "-") == 0) {
1403                                 snprintf(err_buf, sizeof err_buf,
1404                                         "Invalid --files-from remote filename\n");
1405                                 return 0;
1406                         }
1407                 } else {
1408                         if (sanitize_paths)
1409                                 files_from = sanitize_path(NULL, files_from, NULL, 0);
1410                         if (server_filter_list.head) {
1411                                 if (!*files_from)
1412                                         goto options_rejected;
1413                                 clean_fname(files_from, 1);
1414                                 if (check_filter(&server_filter_list, files_from, 0) < 0)
1415                                         goto options_rejected;
1416                         }
1417                         filesfrom_fd = open(files_from, O_RDONLY|O_BINARY);
1418                         if (filesfrom_fd < 0) {
1419                                 snprintf(err_buf, sizeof err_buf,
1420                                         "failed to open files-from file %s: %s\n",
1421                                         files_from, strerror(errno));
1422                                 return 0;
1423                         }
1424                 }
1425         }
1426
1427         am_starting_up = 0;
1428
1429         return 1;
1430 }
1431
1432
1433 /**
1434  * Construct a filtered list of options to pass through from the
1435  * client to the server.
1436  *
1437  * This involves setting options that will tell the server how to
1438  * behave, and also filtering out options that are processed only
1439  * locally.
1440  **/
1441 void server_options(char **args,int *argc)
1442 {
1443         static char argstr[64];
1444         int ac = *argc;
1445         char *arg;
1446
1447         int i, x;
1448
1449         if (blocking_io == -1)
1450                 blocking_io = 0;
1451
1452         /* This should always remain first on the server's command-line. */
1453         args[ac++] = "--server";
1454
1455         if (daemon_over_rsh) {
1456                 args[ac++] = "--daemon";
1457                 *argc = ac;
1458                 /* if we're passing --daemon, we're done */
1459                 return;
1460         }
1461
1462         if (!am_sender)
1463                 args[ac++] = "--sender";
1464
1465         x = 1;
1466         argstr[0] = '-';
1467         for (i = 0; i < verbose; i++)
1468                 argstr[x++] = 'v';
1469
1470         /* the -q option is intentionally left out */
1471         if (make_backups)
1472                 argstr[x++] = 'b';
1473         if (update_only)
1474                 argstr[x++] = 'u';
1475         if (!do_xfers) /* NOT "dry_run"! */
1476                 argstr[x++] = 'n';
1477         if (preserve_links)
1478                 argstr[x++] = 'l';
1479         if (copy_links)
1480                 argstr[x++] = 'L';
1481         if (xfer_dirs > (recurse || !delete_mode || !am_sender))
1482                 argstr[x++] = 'd';
1483         if (am_sender) {
1484                 if (keep_dirlinks)
1485                         argstr[x++] = 'K';
1486                 if (prune_empty_dirs)
1487                         argstr[x++] = 'm';
1488                 if (omit_dir_times == 2)
1489                         argstr[x++] = 'O';
1490         }
1491
1492         if (whole_file > 0)
1493                 argstr[x++] = 'W';
1494         /* We don't need to send --no-whole-file, because it's the
1495          * default for remote transfers, and in any case old versions
1496          * of rsync will not understand it. */
1497
1498         if (preserve_hard_links)
1499                 argstr[x++] = 'H';
1500         if (preserve_uid)
1501                 argstr[x++] = 'o';
1502         if (preserve_gid)
1503                 argstr[x++] = 'g';
1504         if (preserve_devices) /* ignore preserve_specials here */
1505                 argstr[x++] = 'D';
1506         if (preserve_times)
1507                 argstr[x++] = 't';
1508         if (preserve_perms)
1509                 argstr[x++] = 'p';
1510         else if (preserve_executability && am_sender)
1511                 argstr[x++] = 'E';
1512         if (recurse)
1513                 argstr[x++] = 'r';
1514         if (always_checksum)
1515                 argstr[x++] = 'c';
1516         if (cvs_exclude)
1517                 argstr[x++] = 'C';
1518         if (ignore_times)
1519                 argstr[x++] = 'I';
1520         if (relative_paths)
1521                 argstr[x++] = 'R';
1522         if (one_file_system) {
1523                 argstr[x++] = 'x';
1524                 if (one_file_system > 1)
1525                         argstr[x++] = 'x';
1526         }
1527         if (sparse_files)
1528                 argstr[x++] = 'S';
1529         if (do_compression)
1530                 argstr[x++] = 'z';
1531
1532         /* This is a complete hack - blame Rusty.  FIXME!
1533          * This hack is only needed for older rsync versions that
1534          * don't understand the --list-only option. */
1535         if (list_only == 1 && !recurse)
1536                 argstr[x++] = 'r';
1537
1538         argstr[x] = '\0';
1539
1540         if (x != 1)
1541                 args[ac++] = argstr;
1542
1543         if (list_only > 1)
1544                 args[ac++] = "--list-only";
1545
1546         /* This makes sure that the remote rsync can handle deleting with -d
1547          * sans -r because the --no-r option was added at the same time. */
1548         if (xfer_dirs && !recurse && delete_mode && am_sender)
1549                 args[ac++] = "--no-r";
1550
1551         if (do_compression && def_compress_level != Z_DEFAULT_COMPRESSION) {
1552                 if (asprintf(&arg, "--compress-level=%d", def_compress_level) < 0)
1553                         goto oom;
1554                 args[ac++] = arg;
1555         }
1556
1557         if (preserve_devices) {
1558                 /* Note: sending "--devices" would not be backward-compatible. */
1559                 if (!preserve_specials)
1560                         args[ac++] = "--no-specials"; /* -D is already set. */
1561         } else if (preserve_specials)
1562                 args[ac++] = "--specials";
1563
1564         /* The server side doesn't use our log-format, but in certain
1565          * circumstances they need to know a little about the option. */
1566         if (log_format && am_sender) {
1567                 if (log_format_has_i > 1)
1568                         args[ac++] = "--log-format=%i%I";
1569                 else if (log_format_has_i)
1570                         args[ac++] = "--log-format=%i";
1571                 else if (log_format_has_o_or_i)
1572                         args[ac++] = "--log-format=%o";
1573                 else if (!verbose)
1574                         args[ac++] = "--log-format=X";
1575         }
1576
1577         if (block_size) {
1578                 if (asprintf(&arg, "-B%lu", block_size) < 0)
1579                         goto oom;
1580                 args[ac++] = arg;
1581         }
1582
1583         if (max_delete && am_sender) {
1584                 if (asprintf(&arg, "--max-delete=%d", max_delete) < 0)
1585                         goto oom;
1586                 args[ac++] = arg;
1587         }
1588
1589         if (min_size && am_sender) {
1590                 args[ac++] = "--min-size";
1591                 args[ac++] = min_size_arg;
1592         }
1593
1594         if (max_size && am_sender) {
1595                 args[ac++] = "--max-size";
1596                 args[ac++] = max_size_arg;
1597         }
1598
1599         if (io_timeout) {
1600                 if (asprintf(&arg, "--timeout=%d", io_timeout) < 0)
1601                         goto oom;
1602                 args[ac++] = arg;
1603         }
1604
1605         if (bwlimit) {
1606                 if (asprintf(&arg, "--bwlimit=%d", bwlimit) < 0)
1607                         goto oom;
1608                 args[ac++] = arg;
1609         }
1610
1611         if (backup_dir) {
1612                 args[ac++] = "--backup-dir";
1613                 args[ac++] = backup_dir;
1614         }
1615
1616         /* Only send --suffix if it specifies a non-default value. */
1617         if (strcmp(backup_suffix, backup_dir ? "" : BACKUP_SUFFIX) != 0) {
1618                 /* We use the following syntax to avoid weirdness with '~'. */
1619                 if (asprintf(&arg, "--suffix=%s", backup_suffix) < 0)
1620                         goto oom;
1621                 args[ac++] = arg;
1622         }
1623
1624         if (am_sender) {
1625                 if (delete_excluded)
1626                         args[ac++] = "--delete-excluded";
1627                 else if (delete_before == 1 || delete_after)
1628                         args[ac++] = "--delete";
1629                 if (delete_before > 1)
1630                         args[ac++] = "--delete-before";
1631                 if (delete_during)
1632                         args[ac++] = "--delete-during";
1633                 if (delete_after)
1634                         args[ac++] = "--delete-after";
1635                 if (force_delete)
1636                         args[ac++] = "--force";
1637                 if (write_batch < 0)
1638                         args[ac++] = "--only-write-batch=X";
1639                 if (am_root > 1)
1640                         args[ac++] = "--super";
1641         }
1642
1643         if (size_only)
1644                 args[ac++] = "--size-only";
1645
1646         if (modify_window_set) {
1647                 if (asprintf(&arg, "--modify-window=%d", modify_window) < 0)
1648                         goto oom;
1649                 args[ac++] = arg;
1650         }
1651
1652         if (checksum_seed) {
1653                 if (asprintf(&arg, "--checksum-seed=%d", checksum_seed) < 0)
1654                         goto oom;
1655                 args[ac++] = arg;
1656         }
1657
1658         if (partial_dir && am_sender) {
1659                 if (partial_dir != tmp_partialdir) {
1660                         args[ac++] = "--partial-dir";
1661                         args[ac++] = partial_dir;
1662                 }
1663                 if (delay_updates)
1664                         args[ac++] = "--delay-updates";
1665         } else if (keep_partial && am_sender)
1666                 args[ac++] = "--partial";
1667
1668         if (ignore_errors)
1669                 args[ac++] = "--ignore-errors";
1670
1671         if (copy_unsafe_links)
1672                 args[ac++] = "--copy-unsafe-links";
1673
1674         if (safe_symlinks)
1675                 args[ac++] = "--safe-links";
1676
1677         if (numeric_ids)
1678                 args[ac++] = "--numeric-ids";
1679
1680         if (ignore_existing && am_sender)
1681                 args[ac++] = "--ignore-existing";
1682
1683         /* Backward compatibility: send --existing, not --ignore-non-existing. */
1684         if (ignore_non_existing && am_sender)
1685                 args[ac++] = "--existing";
1686
1687         if (append_mode)
1688                 args[ac++] = "--append";
1689         else if (inplace)
1690                 args[ac++] = "--inplace";
1691
1692         if (tmpdir && am_sender) {
1693                 args[ac++] = "--temp-dir";
1694                 args[ac++] = tmpdir;
1695         }
1696
1697         if (basis_dir[0] && am_sender) {
1698                 /* the server only needs this option if it is not the sender,
1699                  *   and it may be an older version that doesn't know this
1700                  *   option, so don't send it if client is the sender.
1701                  */
1702                 int i;
1703                 for (i = 0; i < basis_dir_cnt; i++) {
1704                         args[ac++] = dest_option;
1705                         args[ac++] = basis_dir[i];
1706                 }
1707         }
1708
1709         if (files_from && (!am_sender || filesfrom_host)) {
1710                 if (filesfrom_host) {
1711                         args[ac++] = "--files-from";
1712                         args[ac++] = files_from;
1713                         if (eol_nulls)
1714                                 args[ac++] = "--from0";
1715                 } else {
1716                         args[ac++] = "--files-from=-";
1717                         args[ac++] = "--from0";
1718                 }
1719                 if (!relative_paths)
1720                         args[ac++] = "--no-relative";
1721         }
1722         if (relative_paths && !implied_dirs && !am_sender)
1723                 args[ac++] = "--no-implied-dirs";
1724
1725         if (fuzzy_basis && am_sender)
1726                 args[ac++] = "--fuzzy";
1727
1728         if (remove_sent_files)
1729                 args[ac++] = "--remove-sent-files";
1730
1731         *argc = ac;
1732         return;
1733
1734     oom:
1735         out_of_memory("server_options");
1736 }
1737
1738 /* Look for a HOST specfication of the form "HOST:PATH", "HOST::PATH", or
1739  * "rsync://HOST:PORT/PATH".  If found, *host_ptr will be set to some allocated
1740  * memory with the HOST.  If a daemon-accessing spec was specified, the value
1741  * of *port_ptr will contain a non-0 port number, otherwise it will be set to
1742  * 0.  The return value is a pointer to the PATH.  Note that the HOST spec can
1743  * be an IPv6 literal address enclosed in '[' and ']' (such as "[::1]" or
1744  * "[::ffff:127.0.0.1]") which is returned without the '[' and ']'. */
1745 char *check_for_hostspec(char *s, char **host_ptr, int *port_ptr)
1746 {
1747         char *p;
1748         int not_host;
1749
1750         if (port_ptr && strncasecmp(URL_PREFIX, s, strlen(URL_PREFIX)) == 0) {
1751                 char *path;
1752                 int hostlen;
1753                 s += strlen(URL_PREFIX);
1754                 if ((p = strchr(s, '/')) != NULL) {
1755                         hostlen = p - s;
1756                         path = p + 1;
1757                 } else {
1758                         hostlen = strlen(s);
1759                         path = "";
1760                 }
1761                 if (*s == '[' && (p = strchr(s, ']')) != NULL) {
1762                         s++;
1763                         hostlen = p - s;
1764                         if (p[1] == ':')
1765                                 *port_ptr = atoi(p+2);
1766                 } else {
1767                         if ((p = strchr(s, ':')) != NULL) {
1768                                 hostlen = p - s;
1769                                 *port_ptr = atoi(p+1);
1770                         }
1771                 }
1772                 if (!*port_ptr)
1773                         *port_ptr = RSYNC_PORT;
1774                 *host_ptr = new_array(char, hostlen + 1);
1775                 strlcpy(*host_ptr, s, hostlen + 1);
1776                 return path;
1777         }
1778
1779         if (*s == '[' && (p = strchr(s, ']')) != NULL && p[1] == ':') {
1780                 s++;
1781                 *p = '\0';
1782                 not_host = strchr(s, '/') || !strchr(s, ':');
1783                 *p = ']';
1784                 if (not_host)
1785                         return NULL;
1786                 p++;
1787         } else {
1788                 if (!(p = strchr(s, ':')))
1789                         return NULL;
1790                 *p = '\0';
1791                 not_host = strchr(s, '/') != NULL;
1792                 *p = ':';
1793                 if (not_host)
1794                         return NULL;
1795         }
1796
1797         *host_ptr = new_array(char, p - s + 1);
1798         strlcpy(*host_ptr, s, p - s + 1);
1799
1800         if (p[1] == ':') {
1801                 if (port_ptr && !*port_ptr)
1802                         *port_ptr = RSYNC_PORT;
1803                 return p + 2;
1804         }
1805         if (port_ptr)
1806                 *port_ptr = 0;
1807
1808         return p + 1;
1809 }