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