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