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