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