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