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