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