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