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