2 * Command-line (and received via daemon-socket) option parsing.
4 * Copyright (C) 1998-2001 Andrew Tridgell <tridge@samba.org>
5 * Copyright (C) 2000, 2001, 2002 Martin Pool <mbp@samba.org>
6 * Copyright (C) 2002-2007 Wayne Davison
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; either version 3 of the License, or
11 * (at your option) any later version.
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
18 * You should have received a copy of the GNU General Public License along
19 * with this program; if not, visit the http://fsf.org website.
24 #include "zlib/zlib.h"
27 extern int sanitize_paths;
28 extern int daemon_over_rsh;
29 extern struct filter_list_struct filter_list;
30 extern struct filter_list_struct server_filter_list;
35 * If 1, send the whole file as literal data rather than trying to
36 * create an incremental diff.
38 * If -1, then look at whether we're local or remote and go by that.
40 * @sa disable_deltas_p()
45 int keep_dirlinks = 0;
46 int copy_dirlinks = 0;
48 int preserve_links = 0;
49 int preserve_hard_links = 0;
50 int preserve_acls = 0;
51 int preserve_xattrs = 0;
52 int preserve_perms = 0;
53 int preserve_executability = 0;
54 int preserve_devices = 0;
55 int preserve_specials = 0;
58 int preserve_times = 0;
65 int delete_during = 0;
66 int delete_before = 0;
68 int delete_excluded = 0;
69 int remove_source_files = 0;
70 int one_file_system = 0;
71 int protocol_version = PROTOCOL_VERSION;
73 int do_compression = 0;
74 int def_compress_level = Z_DEFAULT_COMPRESSION;
75 int am_root = 0; /* 0 = normal, 1 = root, 2 = --super, -1 = --fake-super */
79 int am_starting_up = 1;
80 int relative_paths = -1;
83 int allow_8bit_chars = 0;
87 int prune_empty_dirs = 0;
89 char *files_from = NULL;
90 int filesfrom_fd = -1;
91 char *filesfrom_host = NULL;
94 int human_readable = 0;
96 int allow_inc_recurse = 1;
101 int keep_partial = 0;
102 int safe_symlinks = 0;
103 int copy_unsafe_links = 0;
105 int daemon_bwlimit = 0;
108 size_t bwlimit_writemax = 0;
109 int ignore_existing = 0;
110 int ignore_non_existing = 0;
111 int need_messages_from_generator = 0;
112 int max_delete = INT_MIN;
115 int ignore_errors = 0;
116 int modify_window = 0;
117 int blocking_io = -1;
118 int checksum_seed = 0;
120 int delay_updates = 0;
121 long block_size = 0; /* "long" because popt can't set an int32. */
122 char *skip_compress = NULL;
124 /** Network address family. **/
127 = 0; /* Any protocol */
129 = AF_INET; /* Must use IPv4 */
133 # define AF_INET6 AF_INET /* make -6 option a no-op */
136 /** Do not go into the background when run as --daemon. Good
137 * for debugging and required for running as a service on W32,
138 * or under Unix process-monitors. **/
140 #if defined _WIN32 || defined __WIN32__
148 int backup_dir_len = 0;
149 int backup_suffix_len;
150 unsigned int backup_dir_remainder;
152 char *backup_suffix = NULL;
154 char *partial_dir = NULL;
155 char *basis_dir[MAX_BASIS_DIRS+1];
156 char *config_file = NULL;
157 char *shell_cmd = NULL;
158 char *logfile_name = NULL;
159 char *logfile_format = NULL;
160 char *stdout_format = NULL;
161 char *password_file = NULL;
162 char *rsync_path = RSYNC_PATH;
163 char *backup_dir = NULL;
164 char backup_dir_buf[MAXPATHLEN];
165 char *sockopts = NULL;
167 int compare_dest = 0;
170 int basis_dir_cnt = 0;
171 char *dest_option = NULL;
176 int log_before_transfer = 0;
177 int stdout_format_has_i = 0;
178 int stdout_format_has_o_or_i = 0;
179 int logfile_format_has_i = 0;
180 int logfile_format_has_o_or_i = 0;
181 int always_checksum = 0;
184 #define MAX_BATCH_NAME_LEN 256 /* Must be less than MAXPATHLEN-13 */
185 char *batch_name = NULL;
188 int need_unsorted_flist = 0;
189 char *iconv_opt = ICONV_OPTION;
192 struct chmod_mode_struct *chmod_modes = NULL;
194 static int daemon_opt; /* sets am_daemon after option error-reporting */
195 static int omit_dir_times = 0;
196 static int F_option_cnt = 0;
197 static int modify_window_set;
198 static int itemize_changes = 0;
199 static int refused_delete, refused_archive_part, refused_compress;
200 static int refused_partial, refused_progress, refused_delete_before;
201 static int refused_delete_during;
202 static int refused_inplace;
203 static char *max_size_arg, *min_size_arg;
204 static char tmp_partialdir[] = ".~tmp~";
206 /** Local address to bind. As a character string because it's
207 * interpreted by the IPv6 layer: should be a numeric IP4 or IP6
208 * address, or a hostname. **/
212 static void print_rsync_version(enum logcode f)
214 char *subprotocol = "";
215 char const *got_socketpair = "no ";
216 char const *have_inplace = "no ";
217 char const *hardlinks = "no ";
218 char const *acls = "no ";
219 char const *xattrs = "no ";
220 char const *links = "no ";
221 char const *iconv = "no ";
222 char const *ipv6 = "no ";
223 STRUCT_STAT *dumstat;
225 #if SUBPROTOCOL_VERSION != 0
226 asprintf(&subprotocol, ".PR%d", SUBPROTOCOL_VERSION);
228 #ifdef HAVE_SOCKETPAIR
231 #ifdef HAVE_FTRUNCATE
234 #ifdef SUPPORT_HARD_LINKS
240 #ifdef SUPPORT_XATTRS
253 rprintf(f, "%s version %s protocol version %d%s\n",
254 RSYNC_NAME, RSYNC_VERSION, PROTOCOL_VERSION, subprotocol);
255 rprintf(f, "Copyright (C) 1996-2007 by Andrew Tridgell, Wayne Davison, and others.\n");
256 rprintf(f, "Web site: http://rsync.samba.org/\n");
257 rprintf(f, "Capabilities:\n");
258 rprintf(f, " %d-bit files, %d-bit inums, %d-bit timestamps, %d-bit long ints,\n",
259 (int)(sizeof (OFF_T) * 8),
260 (int)(sizeof dumstat->st_ino * 8), /* Don't check ino_t! */
261 (int)(sizeof (time_t) * 8),
262 (int)(sizeof (int64) * 8));
263 rprintf(f, " %ssocketpairs, %shardlinks, %ssymlinks, %sIPv6, batchfiles, %sinplace,\n",
264 got_socketpair, hardlinks, links, ipv6, have_inplace);
265 rprintf(f, " %sappend, %sACLs, %sxattrs, %siconv\n",
266 have_inplace, acls, xattrs, iconv);
268 #ifdef MAINTAINER_MODE
269 rprintf(f, "Panic Action: \"%s\"\n", get_panic_action());
273 rprintf(f, "WARNING: no 64-bit integers on this platform!\n");
275 if (sizeof (int64) != SIZEOF_INT64) {
277 "WARNING: size mismatch in SIZEOF_INT64 define (%d != %d)\n",
278 (int) SIZEOF_INT64, (int) sizeof (int64));
282 rprintf(f,"rsync comes with ABSOLUTELY NO WARRANTY. This is free software, and you\n");
283 rprintf(f,"are welcome to redistribute it under certain conditions. See the GNU\n");
284 rprintf(f,"General Public Licence for details.\n");
288 void usage(enum logcode F)
290 print_rsync_version(F);
293 rprintf(F,"rsync is a file transfer program capable of efficient remote update\n");
294 rprintf(F,"via a fast differencing algorithm.\n");
297 rprintf(F,"Usage: rsync [OPTION]... SRC [SRC]... DEST\n");
298 rprintf(F," or rsync [OPTION]... SRC [SRC]... [USER@]HOST:DEST\n");
299 rprintf(F," or rsync [OPTION]... SRC [SRC]... [USER@]HOST::DEST\n");
300 rprintf(F," or rsync [OPTION]... SRC [SRC]... rsync://[USER@]HOST[:PORT]/DEST\n");
301 rprintf(F," or rsync [OPTION]... [USER@]HOST:SRC [DEST]\n");
302 rprintf(F," or rsync [OPTION]... [USER@]HOST::SRC [DEST]\n");
303 rprintf(F," or rsync [OPTION]... rsync://[USER@]HOST[:PORT]/SRC [DEST]\n");
304 rprintf(F,"The ':' usages connect via remote shell, while '::' & 'rsync://' usages connect\n");
305 rprintf(F,"to an rsync daemon, and require SRC or DEST to start with a module name.\n");
307 rprintf(F,"Options\n");
308 rprintf(F," -v, --verbose increase verbosity\n");
309 rprintf(F," -q, --quiet suppress non-error messages\n");
310 rprintf(F," --no-motd suppress daemon-mode MOTD (see manpage caveat)\n");
311 rprintf(F," -c, --checksum skip based on checksum, not mod-time & size\n");
312 rprintf(F," -a, --archive archive mode; equals -rlptgoD (no -H,-A,-X)\n");
313 rprintf(F," --no-OPTION turn off an implied OPTION (e.g. --no-D)\n");
314 rprintf(F," -r, --recursive recurse into directories\n");
315 rprintf(F," -R, --relative use relative path names\n");
316 rprintf(F," --no-implied-dirs don't send implied dirs with --relative\n");
317 rprintf(F," -b, --backup make backups (see --suffix & --backup-dir)\n");
318 rprintf(F," --backup-dir=DIR make backups into hierarchy based in DIR\n");
319 rprintf(F," --suffix=SUFFIX set backup suffix (default %s w/o --backup-dir)\n",BACKUP_SUFFIX);
320 rprintf(F," -u, --update skip files that are newer on the receiver\n");
321 rprintf(F," --inplace update destination files in-place (SEE MAN PAGE)\n");
322 rprintf(F," --append append data onto shorter files\n");
323 rprintf(F," --append-verify like --append, but with old data in file checksum\n");
324 rprintf(F," -d, --dirs transfer directories without recursing\n");
325 rprintf(F," -l, --links copy symlinks as symlinks\n");
326 rprintf(F," -L, --copy-links transform symlink into referent file/dir\n");
327 rprintf(F," --copy-unsafe-links only \"unsafe\" symlinks are transformed\n");
328 rprintf(F," --safe-links ignore symlinks that point outside the source tree\n");
329 rprintf(F," -k, --copy-dirlinks transform symlink to a dir into referent dir\n");
330 rprintf(F," -K, --keep-dirlinks treat symlinked dir on receiver as dir\n");
331 rprintf(F," -H, --hard-links preserve hard links\n");
332 rprintf(F," -p, --perms preserve permissions\n");
333 rprintf(F," -E, --executability preserve the file's executability\n");
334 rprintf(F," --chmod=CHMOD affect file and/or directory permissions\n");
336 rprintf(F," -A, --acls preserve ACLs (implies --perms)\n");
338 #ifdef SUPPORT_XATTRS
339 rprintf(F," -X, --xattrs preserve extended attributes\n");
341 rprintf(F," -o, --owner preserve owner (super-user only)\n");
342 rprintf(F," -g, --group preserve group\n");
343 rprintf(F," --devices preserve device files (super-user only)\n");
344 rprintf(F," --specials preserve special files\n");
345 rprintf(F," -D same as --devices --specials\n");
346 rprintf(F," -t, --times preserve modification times\n");
347 rprintf(F," -O, --omit-dir-times omit directories from --times\n");
348 rprintf(F," --super receiver attempts super-user activities\n");
349 #ifdef SUPPORT_XATTRS
350 rprintf(F," --fake-super store/recover privileged attrs using xattrs\n");
352 rprintf(F," -S, --sparse handle sparse files efficiently\n");
353 rprintf(F," -n, --dry-run show what would have been transferred\n");
354 rprintf(F," -W, --whole-file copy files whole (without rsync algorithm)\n");
355 rprintf(F," -x, --one-file-system don't cross filesystem boundaries\n");
356 rprintf(F," -B, --block-size=SIZE force a fixed checksum block-size\n");
357 rprintf(F," -e, --rsh=COMMAND specify the remote shell to use\n");
358 rprintf(F," --rsync-path=PROGRAM specify the rsync to run on the remote machine\n");
359 rprintf(F," --existing skip creating new files on receiver\n");
360 rprintf(F," --ignore-existing skip updating files that already exist on receiver\n");
361 rprintf(F," --remove-source-files sender removes synchronized files (non-dirs)\n");
362 rprintf(F," --del an alias for --delete-during\n");
363 rprintf(F," --delete delete extraneous files from destination dirs\n");
364 rprintf(F," --delete-before receiver deletes before transfer, not during\n");
365 rprintf(F," --delete-during receiver deletes during transfer (default)\n");
366 rprintf(F," --delete-delay find deletions during, delete after\n");
367 rprintf(F," --delete-after receiver deletes after transfer, not during\n");
368 rprintf(F," --delete-excluded also delete excluded files from destination dirs\n");
369 rprintf(F," --ignore-errors delete even if there are I/O errors\n");
370 rprintf(F," --force force deletion of directories even if not empty\n");
371 rprintf(F," --max-delete=NUM don't delete more than NUM files\n");
372 rprintf(F," --max-size=SIZE don't transfer any file larger than SIZE\n");
373 rprintf(F," --min-size=SIZE don't transfer any file smaller than SIZE\n");
374 rprintf(F," --partial keep partially transferred files\n");
375 rprintf(F," --partial-dir=DIR put a partially transferred file into DIR\n");
376 rprintf(F," --delay-updates put all updated files into place at transfer's end\n");
377 rprintf(F," -m, --prune-empty-dirs prune empty directory chains from the file-list\n");
378 rprintf(F," --numeric-ids don't map uid/gid values by user/group name\n");
379 rprintf(F," --timeout=TIME set I/O timeout in seconds\n");
380 rprintf(F," -I, --ignore-times don't skip files that match in size and mod-time\n");
381 rprintf(F," --size-only skip files that match in size\n");
382 rprintf(F," --modify-window=NUM compare mod-times with reduced accuracy\n");
383 rprintf(F," -T, --temp-dir=DIR create temporary files in directory DIR\n");
384 rprintf(F," -y, --fuzzy find similar file for basis if no dest file\n");
385 rprintf(F," --compare-dest=DIR also compare destination files relative to DIR\n");
386 rprintf(F," --copy-dest=DIR ... and include copies of unchanged files\n");
387 rprintf(F," --link-dest=DIR hardlink to files in DIR when unchanged\n");
388 rprintf(F," -z, --compress compress file data during the transfer\n");
389 rprintf(F," --compress-level=NUM explicitly set compression level\n");
390 rprintf(F," --skip-compress=LIST skip compressing files with a suffix in LIST\n");
391 rprintf(F," -C, --cvs-exclude auto-ignore files the same way CVS does\n");
392 rprintf(F," -f, --filter=RULE add a file-filtering RULE\n");
393 rprintf(F," -F same as --filter='dir-merge /.rsync-filter'\n");
394 rprintf(F," repeated: --filter='- .rsync-filter'\n");
395 rprintf(F," --exclude=PATTERN exclude files matching PATTERN\n");
396 rprintf(F," --exclude-from=FILE read exclude patterns from FILE\n");
397 rprintf(F," --include=PATTERN don't exclude files matching PATTERN\n");
398 rprintf(F," --include-from=FILE read include patterns from FILE\n");
399 rprintf(F," --files-from=FILE read list of source-file names from FILE\n");
400 rprintf(F," -0, --from0 all *-from/filter files are delimited by 0s\n");
401 rprintf(F," -s, --protect-args no space-splitting; only wildcard special-chars\n");
402 rprintf(F," --address=ADDRESS bind address for outgoing socket to daemon\n");
403 rprintf(F," --port=PORT specify double-colon alternate port number\n");
404 rprintf(F," --sockopts=OPTIONS specify custom TCP options\n");
405 rprintf(F," --blocking-io use blocking I/O for the remote shell\n");
406 rprintf(F," --stats give some file-transfer stats\n");
407 rprintf(F," -8, --8-bit-output leave high-bit chars unescaped in output\n");
408 rprintf(F," -h, --human-readable output numbers in a human-readable format\n");
409 rprintf(F," --progress show progress during transfer\n");
410 rprintf(F," -P same as --partial --progress\n");
411 rprintf(F," -i, --itemize-changes output a change-summary for all updates\n");
412 rprintf(F," --out-format=FORMAT output updates using the specified FORMAT\n");
413 rprintf(F," --log-file=FILE log what we're doing to the specified FILE\n");
414 rprintf(F," --log-file-format=FMT log updates using the specified FMT\n");
415 rprintf(F," --password-file=FILE read daemon-access password from FILE\n");
416 rprintf(F," --list-only list the files instead of copying them\n");
417 rprintf(F," --bwlimit=KBPS limit I/O bandwidth; KBytes per second\n");
418 rprintf(F," --write-batch=FILE write a batched update to FILE\n");
419 rprintf(F," --only-write-batch=FILE like --write-batch but w/o updating destination\n");
420 rprintf(F," --read-batch=FILE read a batched update from FILE\n");
421 rprintf(F," --protocol=NUM force an older protocol version to be used\n");
423 rprintf(F," --iconv=CONVERT_SPEC request charset conversion of filesnames\n");
425 rprintf(F," -4, --ipv4 prefer IPv4\n");
426 rprintf(F," -6, --ipv6 prefer IPv6\n");
427 rprintf(F," --version print version number\n");
428 rprintf(F,"(-h) --help show this help (-h works with no other options)\n");
431 rprintf(F,"Use \"rsync --daemon --help\" to see the daemon-mode command-line options.\n");
432 rprintf(F,"Please see the rsync(1) and rsyncd.conf(5) man pages for full documentation.\n");
433 rprintf(F,"See http://rsync.samba.org/ for updates, bug reports, and answers\n");
436 enum {OPT_VERSION = 1000, OPT_DAEMON, OPT_SENDER, OPT_EXCLUDE, OPT_EXCLUDE_FROM,
437 OPT_FILTER, OPT_COMPARE_DEST, OPT_COPY_DEST, OPT_LINK_DEST, OPT_HELP,
438 OPT_INCLUDE, OPT_INCLUDE_FROM, OPT_MODIFY_WINDOW, OPT_MIN_SIZE, OPT_CHMOD,
439 OPT_READ_BATCH, OPT_WRITE_BATCH, OPT_ONLY_WRITE_BATCH, OPT_MAX_SIZE,
440 OPT_NO_D, OPT_APPEND,
441 OPT_SERVER, OPT_REFUSED_BASE = 9000};
443 static struct poptOption long_options[] = {
444 /* longName, shortName, argInfo, argPtr, value, descrip, argDesc */
445 {"help", 0, POPT_ARG_NONE, 0, OPT_HELP, 0, 0 },
446 {"version", 0, POPT_ARG_NONE, 0, OPT_VERSION, 0, 0},
447 {"verbose", 'v', POPT_ARG_NONE, 0, 'v', 0, 0 },
448 {"no-verbose", 0, POPT_ARG_VAL, &verbose, 0, 0, 0 },
449 {"no-v", 0, POPT_ARG_VAL, &verbose, 0, 0, 0 },
450 {"quiet", 'q', POPT_ARG_NONE, 0, 'q', 0, 0 },
451 {"motd", 0, POPT_ARG_VAL, &output_motd, 1, 0, 0 },
452 {"no-motd", 0, POPT_ARG_VAL, &output_motd, 0, 0, 0 },
453 {"stats", 0, POPT_ARG_NONE, &do_stats, 0, 0, 0 },
454 {"human-readable", 'h', POPT_ARG_NONE, 0, 'h', 0, 0},
455 {"no-human-readable",0, POPT_ARG_VAL, &human_readable, 0, 0, 0},
456 {"no-h", 0, POPT_ARG_VAL, &human_readable, 0, 0, 0},
457 {"dry-run", 'n', POPT_ARG_NONE, &dry_run, 0, 0, 0 },
458 {"archive", 'a', POPT_ARG_NONE, 0, 'a', 0, 0 },
459 {"recursive", 'r', POPT_ARG_VAL, &recurse, 2, 0, 0 },
460 {"no-recursive", 0, POPT_ARG_VAL, &recurse, 0, 0, 0 },
461 {"no-r", 0, POPT_ARG_VAL, &recurse, 0, 0, 0 },
462 {"inc-recursive", 0, POPT_ARG_VAL, &allow_inc_recurse, 1, 0, 0 },
463 {"no-inc-recursive", 0, POPT_ARG_VAL, &allow_inc_recurse, 0, 0, 0 },
464 {"ir", 0, POPT_ARG_VAL, &allow_inc_recurse, 1, 0, 0 },
465 {"no-ir", 0, POPT_ARG_VAL, &allow_inc_recurse, 0, 0, 0 },
466 {"dirs", 'd', POPT_ARG_VAL, &xfer_dirs, 2, 0, 0 },
467 {"no-dirs", 0, POPT_ARG_VAL, &xfer_dirs, 0, 0, 0 },
468 {"no-d", 0, POPT_ARG_VAL, &xfer_dirs, 0, 0, 0 },
469 {"perms", 'p', POPT_ARG_VAL, &preserve_perms, 1, 0, 0 },
470 {"no-perms", 0, POPT_ARG_VAL, &preserve_perms, 0, 0, 0 },
471 {"no-p", 0, POPT_ARG_VAL, &preserve_perms, 0, 0, 0 },
472 {"executability", 'E', POPT_ARG_NONE, &preserve_executability, 0, 0, 0 },
473 {"acls", 'A', POPT_ARG_NONE, 0, 'A', 0, 0 },
474 {"no-acls", 0, POPT_ARG_VAL, &preserve_acls, 0, 0, 0 },
475 {"no-A", 0, POPT_ARG_VAL, &preserve_acls, 0, 0, 0 },
476 {"xattrs", 'X', POPT_ARG_NONE, 0, 'X', 0, 0 },
477 {"no-xattrs", 0, POPT_ARG_VAL, &preserve_xattrs, 0, 0, 0 },
478 {"no-X", 0, POPT_ARG_VAL, &preserve_xattrs, 0, 0, 0 },
479 {"times", 't', POPT_ARG_VAL, &preserve_times, 2, 0, 0 },
480 {"no-times", 0, POPT_ARG_VAL, &preserve_times, 0, 0, 0 },
481 {"no-t", 0, POPT_ARG_VAL, &preserve_times, 0, 0, 0 },
482 {"omit-dir-times", 'O', POPT_ARG_VAL, &omit_dir_times, 1, 0, 0 },
483 {"no-omit-dir-times",0, POPT_ARG_VAL, &omit_dir_times, 0, 0, 0 },
484 {"no-O", 0, POPT_ARG_VAL, &omit_dir_times, 0, 0, 0 },
485 {"modify-window", 0, POPT_ARG_INT, &modify_window, OPT_MODIFY_WINDOW, 0, 0 },
486 {"super", 0, POPT_ARG_VAL, &am_root, 2, 0, 0 },
487 {"no-super", 0, POPT_ARG_VAL, &am_root, 0, 0, 0 },
488 {"fake-super", 0, POPT_ARG_VAL, &am_root, -1, 0, 0 },
489 {"owner", 'o', POPT_ARG_VAL, &preserve_uid, 1, 0, 0 },
490 {"no-owner", 0, POPT_ARG_VAL, &preserve_uid, 0, 0, 0 },
491 {"no-o", 0, POPT_ARG_VAL, &preserve_uid, 0, 0, 0 },
492 {"group", 'g', POPT_ARG_VAL, &preserve_gid, 1, 0, 0 },
493 {"no-group", 0, POPT_ARG_VAL, &preserve_gid, 0, 0, 0 },
494 {"no-g", 0, POPT_ARG_VAL, &preserve_gid, 0, 0, 0 },
495 {0, 'D', POPT_ARG_NONE, 0, 'D', 0, 0 },
496 {"no-D", 0, POPT_ARG_NONE, 0, OPT_NO_D, 0, 0 },
497 {"devices", 0, POPT_ARG_VAL, &preserve_devices, 1, 0, 0 },
498 {"no-devices", 0, POPT_ARG_VAL, &preserve_devices, 0, 0, 0 },
499 {"specials", 0, POPT_ARG_VAL, &preserve_specials, 1, 0, 0 },
500 {"no-specials", 0, POPT_ARG_VAL, &preserve_specials, 0, 0, 0 },
501 {"links", 'l', POPT_ARG_VAL, &preserve_links, 1, 0, 0 },
502 {"no-links", 0, POPT_ARG_VAL, &preserve_links, 0, 0, 0 },
503 {"no-l", 0, POPT_ARG_VAL, &preserve_links, 0, 0, 0 },
504 {"copy-links", 'L', POPT_ARG_NONE, ©_links, 0, 0, 0 },
505 {"copy-unsafe-links",0, POPT_ARG_NONE, ©_unsafe_links, 0, 0, 0 },
506 {"safe-links", 0, POPT_ARG_NONE, &safe_symlinks, 0, 0, 0 },
507 {"copy-dirlinks", 'k', POPT_ARG_NONE, ©_dirlinks, 0, 0, 0 },
508 {"keep-dirlinks", 'K', POPT_ARG_NONE, &keep_dirlinks, 0, 0, 0 },
509 {"hard-links", 'H', POPT_ARG_NONE, 0, 'H', 0, 0 },
510 {"no-hard-links", 0, POPT_ARG_VAL, &preserve_hard_links, 0, 0, 0 },
511 {"no-H", 0, POPT_ARG_VAL, &preserve_hard_links, 0, 0, 0 },
512 {"relative", 'R', POPT_ARG_VAL, &relative_paths, 1, 0, 0 },
513 {"no-relative", 0, POPT_ARG_VAL, &relative_paths, 0, 0, 0 },
514 {"no-R", 0, POPT_ARG_VAL, &relative_paths, 0, 0, 0 },
515 {"implied-dirs", 0, POPT_ARG_VAL, &implied_dirs, 1, 0, 0 },
516 {"no-implied-dirs", 0, POPT_ARG_VAL, &implied_dirs, 0, 0, 0 },
517 {"chmod", 0, POPT_ARG_STRING, 0, OPT_CHMOD, 0, 0 },
518 {"ignore-times", 'I', POPT_ARG_NONE, &ignore_times, 0, 0, 0 },
519 {"size-only", 0, POPT_ARG_NONE, &size_only, 0, 0, 0 },
520 {"one-file-system", 'x', POPT_ARG_NONE, 0, 'x', 0, 0 },
521 {"update", 'u', POPT_ARG_NONE, &update_only, 0, 0, 0 },
522 {"existing", 0, POPT_ARG_NONE, &ignore_non_existing, 0, 0, 0 },
523 {"ignore-non-existing",0,POPT_ARG_NONE, &ignore_non_existing, 0, 0, 0 },
524 {"ignore-existing", 0, POPT_ARG_NONE, &ignore_existing, 0, 0, 0 },
525 {"max-size", 0, POPT_ARG_STRING, &max_size_arg, OPT_MAX_SIZE, 0, 0 },
526 {"min-size", 0, POPT_ARG_STRING, &min_size_arg, OPT_MIN_SIZE, 0, 0 },
527 {"sparse", 'S', POPT_ARG_NONE, &sparse_files, 0, 0, 0 },
528 {"inplace", 0, POPT_ARG_NONE, &inplace, 0, 0, 0 },
529 {"append", 0, POPT_ARG_NONE, 0, OPT_APPEND, 0, 0 },
530 {"append-verify", 0, POPT_ARG_VAL, &append_mode, 2, 0, 0 },
531 {"no-append", 0, POPT_ARG_VAL, &append_mode, 0, 0, 0 },
532 {"del", 0, POPT_ARG_NONE, &delete_during, 0, 0, 0 },
533 {"delete", 0, POPT_ARG_NONE, &delete_mode, 0, 0, 0 },
534 {"delete-before", 0, POPT_ARG_NONE, &delete_before, 0, 0, 0 },
535 {"delete-during", 0, POPT_ARG_VAL, &delete_during, 1, 0, 0 },
536 {"delete-delay", 0, POPT_ARG_VAL, &delete_during, 2, 0, 0 },
537 {"delete-after", 0, POPT_ARG_NONE, &delete_after, 0, 0, 0 },
538 {"delete-excluded", 0, POPT_ARG_NONE, &delete_excluded, 0, 0, 0 },
539 {"remove-sent-files",0, POPT_ARG_VAL, &remove_source_files, 2, 0, 0 }, /* deprecated */
540 {"remove-source-files",0,POPT_ARG_VAL, &remove_source_files, 1, 0, 0 },
541 {"force", 0, POPT_ARG_NONE, &force_delete, 0, 0, 0 },
542 {"ignore-errors", 0, POPT_ARG_NONE, &ignore_errors, 0, 0, 0 },
543 {"max-delete", 0, POPT_ARG_INT, &max_delete, 0, 0, 0 },
544 {0, 'F', POPT_ARG_NONE, 0, 'F', 0, 0 },
545 {"filter", 'f', POPT_ARG_STRING, 0, OPT_FILTER, 0, 0 },
546 {"exclude", 0, POPT_ARG_STRING, 0, OPT_EXCLUDE, 0, 0 },
547 {"include", 0, POPT_ARG_STRING, 0, OPT_INCLUDE, 0, 0 },
548 {"exclude-from", 0, POPT_ARG_STRING, 0, OPT_EXCLUDE_FROM, 0, 0 },
549 {"include-from", 0, POPT_ARG_STRING, 0, OPT_INCLUDE_FROM, 0, 0 },
550 {"cvs-exclude", 'C', POPT_ARG_NONE, &cvs_exclude, 0, 0, 0 },
551 {"whole-file", 'W', POPT_ARG_VAL, &whole_file, 1, 0, 0 },
552 {"no-whole-file", 0, POPT_ARG_VAL, &whole_file, 0, 0, 0 },
553 {"no-W", 0, POPT_ARG_VAL, &whole_file, 0, 0, 0 },
554 {"checksum", 'c', POPT_ARG_VAL, &always_checksum, 1, 0, 0 },
555 {"no-checksum", 0, POPT_ARG_VAL, &always_checksum, 0, 0, 0 },
556 {"no-c", 0, POPT_ARG_VAL, &always_checksum, 0, 0, 0 },
557 {"block-size", 'B', POPT_ARG_LONG, &block_size, 0, 0, 0 },
558 {"compare-dest", 0, POPT_ARG_STRING, 0, OPT_COMPARE_DEST, 0, 0 },
559 {"copy-dest", 0, POPT_ARG_STRING, 0, OPT_COPY_DEST, 0, 0 },
560 {"link-dest", 0, POPT_ARG_STRING, 0, OPT_LINK_DEST, 0, 0 },
561 {"fuzzy", 'y', POPT_ARG_NONE, &fuzzy_basis, 0, 0, 0 },
562 {"compress", 'z', POPT_ARG_NONE, 0, 'z', 0, 0 },
563 {"no-compress", 0, POPT_ARG_VAL, &do_compression, 0, 0, 0 },
564 {"skip-compress", 0, POPT_ARG_STRING, &skip_compress, 0, 0, 0 },
565 {"no-z", 0, POPT_ARG_VAL, &do_compression, 0, 0, 0 },
566 {"compress-level", 0, POPT_ARG_INT, &def_compress_level, 'z', 0, 0 },
567 {0, 'P', POPT_ARG_NONE, 0, 'P', 0, 0 },
568 {"progress", 0, POPT_ARG_VAL, &do_progress, 1, 0, 0 },
569 {"no-progress", 0, POPT_ARG_VAL, &do_progress, 0, 0, 0 },
570 {"partial", 0, POPT_ARG_VAL, &keep_partial, 1, 0, 0 },
571 {"no-partial", 0, POPT_ARG_VAL, &keep_partial, 0, 0, 0 },
572 {"partial-dir", 0, POPT_ARG_STRING, &partial_dir, 0, 0, 0 },
573 {"delay-updates", 0, POPT_ARG_VAL, &delay_updates, 1, 0, 0 },
574 {"no-delay-updates", 0, POPT_ARG_VAL, &delay_updates, 0, 0, 0 },
575 {"prune-empty-dirs",'m', POPT_ARG_NONE, &prune_empty_dirs, 0, 0, 0 },
576 {"log-file", 0, POPT_ARG_STRING, &logfile_name, 0, 0, 0 },
577 {"log-file-format", 0, POPT_ARG_STRING, &logfile_format, 0, 0, 0 },
578 {"out-format", 0, POPT_ARG_STRING, &stdout_format, 0, 0, 0 },
579 {"log-format", 0, POPT_ARG_STRING, &stdout_format, 0, 0, 0 }, /* DEPRECATED */
580 {"itemize-changes", 'i', POPT_ARG_NONE, 0, 'i', 0, 0 },
581 {"no-itemize-changes",0, POPT_ARG_VAL, &itemize_changes, 0, 0, 0 },
582 {"no-i", 0, POPT_ARG_VAL, &itemize_changes, 0, 0, 0 },
583 {"bwlimit", 0, POPT_ARG_INT, &bwlimit, 0, 0, 0 },
584 {"no-bwlimit", 0, POPT_ARG_VAL, &bwlimit, 0, 0, 0 },
585 {"backup", 'b', POPT_ARG_VAL, &make_backups, 1, 0, 0 },
586 {"no-backup", 0, POPT_ARG_VAL, &make_backups, 0, 0, 0 },
587 {"backup-dir", 0, POPT_ARG_STRING, &backup_dir, 0, 0, 0 },
588 {"suffix", 0, POPT_ARG_STRING, &backup_suffix, 0, 0, 0 },
589 {"list-only", 0, POPT_ARG_VAL, &list_only, 2, 0, 0 },
590 {"read-batch", 0, POPT_ARG_STRING, &batch_name, OPT_READ_BATCH, 0, 0 },
591 {"write-batch", 0, POPT_ARG_STRING, &batch_name, OPT_WRITE_BATCH, 0, 0 },
592 {"only-write-batch", 0, POPT_ARG_STRING, &batch_name, OPT_ONLY_WRITE_BATCH, 0, 0 },
593 {"files-from", 0, POPT_ARG_STRING, &files_from, 0, 0, 0 },
594 {"from0", '0', POPT_ARG_VAL, &eol_nulls, 1, 0, 0},
595 {"no-from0", 0, POPT_ARG_VAL, &eol_nulls, 0, 0, 0},
596 {"protect-args", 's', POPT_ARG_VAL, &protect_args, 1, 0, 0},
597 {"no-protect-args", 0, POPT_ARG_VAL, &protect_args, 0, 0, 0},
598 {"no-s", 0, POPT_ARG_VAL, &protect_args, 0, 0, 0},
599 {"numeric-ids", 0, POPT_ARG_VAL, &numeric_ids, 1, 0, 0 },
600 {"no-numeric-ids", 0, POPT_ARG_VAL, &numeric_ids, 0, 0, 0 },
601 {"timeout", 0, POPT_ARG_INT, &io_timeout, 0, 0, 0 },
602 {"no-timeout", 0, POPT_ARG_VAL, &io_timeout, 0, 0, 0 },
603 {"rsh", 'e', POPT_ARG_STRING, &shell_cmd, 0, 0, 0 },
604 {"rsync-path", 0, POPT_ARG_STRING, &rsync_path, 0, 0, 0 },
605 {"temp-dir", 'T', POPT_ARG_STRING, &tmpdir, 0, 0, 0 },
607 {"iconv", 0, POPT_ARG_STRING, &iconv_opt, 0, 0, 0 },
609 {"ipv4", '4', POPT_ARG_VAL, &default_af_hint, AF_INET, 0, 0 },
610 {"ipv6", '6', POPT_ARG_VAL, &default_af_hint, AF_INET6, 0, 0 },
611 {"8-bit-output", '8', POPT_ARG_NONE, &allow_8bit_chars, 0, 0, 0 },
612 {"qsort", 0, POPT_ARG_NONE, &use_qsort, 0, 0, 0 },
613 {"address", 0, POPT_ARG_STRING, &bind_address, 0, 0, 0 },
614 {"port", 0, POPT_ARG_INT, &rsync_port, 0, 0, 0 },
615 {"sockopts", 0, POPT_ARG_STRING, &sockopts, 0, 0, 0 },
616 {"password-file", 0, POPT_ARG_STRING, &password_file, 0, 0, 0 },
617 {"blocking-io", 0, POPT_ARG_VAL, &blocking_io, 1, 0, 0 },
618 {"no-blocking-io", 0, POPT_ARG_VAL, &blocking_io, 0, 0, 0 },
619 {"protocol", 0, POPT_ARG_INT, &protocol_version, 0, 0, 0 },
620 {"checksum-seed", 0, POPT_ARG_INT, &checksum_seed, 0, 0, 0 },
621 {"server", 0, POPT_ARG_NONE, 0, OPT_SERVER, 0, 0 },
622 {"sender", 0, POPT_ARG_NONE, 0, OPT_SENDER, 0, 0 },
623 /* All the following options switch us into daemon-mode option-parsing. */
624 {"config", 0, POPT_ARG_STRING, 0, OPT_DAEMON, 0, 0 },
625 {"daemon", 0, POPT_ARG_NONE, 0, OPT_DAEMON, 0, 0 },
626 {"detach", 0, POPT_ARG_NONE, 0, OPT_DAEMON, 0, 0 },
627 {"no-detach", 0, POPT_ARG_NONE, 0, OPT_DAEMON, 0, 0 },
631 static void daemon_usage(enum logcode F)
633 print_rsync_version(F);
636 rprintf(F,"Usage: rsync --daemon [OPTION]...\n");
637 rprintf(F," --address=ADDRESS bind to the specified address\n");
638 rprintf(F," --bwlimit=KBPS limit I/O bandwidth; KBytes per second\n");
639 rprintf(F," --config=FILE specify alternate rsyncd.conf file\n");
640 rprintf(F," --no-detach do not detach from the parent\n");
641 rprintf(F," --port=PORT listen on alternate port number\n");
642 rprintf(F," --log-file=FILE override the \"log file\" setting\n");
643 rprintf(F," --log-file-format=FMT override the \"log format\" setting\n");
644 rprintf(F," --sockopts=OPTIONS specify custom TCP options\n");
645 rprintf(F," -v, --verbose increase verbosity\n");
646 rprintf(F," -4, --ipv4 prefer IPv4\n");
647 rprintf(F," -6, --ipv6 prefer IPv6\n");
648 rprintf(F," --help show this help screen\n");
651 rprintf(F,"If you were not trying to invoke rsync as a daemon, avoid using any of the\n");
652 rprintf(F,"daemon-specific rsync options. See also the rsyncd.conf(5) man page.\n");
655 static struct poptOption long_daemon_options[] = {
656 /* longName, shortName, argInfo, argPtr, value, descrip, argDesc */
657 {"address", 0, POPT_ARG_STRING, &bind_address, 0, 0, 0 },
658 {"bwlimit", 0, POPT_ARG_INT, &daemon_bwlimit, 0, 0, 0 },
659 {"config", 0, POPT_ARG_STRING, &config_file, 0, 0, 0 },
660 {"daemon", 0, POPT_ARG_NONE, &daemon_opt, 0, 0, 0 },
661 {"ipv4", '4', POPT_ARG_VAL, &default_af_hint, AF_INET, 0, 0 },
662 {"ipv6", '6', POPT_ARG_VAL, &default_af_hint, AF_INET6, 0, 0 },
663 {"detach", 0, POPT_ARG_VAL, &no_detach, 0, 0, 0 },
664 {"no-detach", 0, POPT_ARG_VAL, &no_detach, 1, 0, 0 },
665 {"log-file", 0, POPT_ARG_STRING, &logfile_name, 0, 0, 0 },
666 {"log-file-format", 0, POPT_ARG_STRING, &logfile_format, 0, 0, 0 },
667 {"port", 0, POPT_ARG_INT, &rsync_port, 0, 0, 0 },
668 {"sockopts", 0, POPT_ARG_STRING, &sockopts, 0, 0, 0 },
669 {"protocol", 0, POPT_ARG_INT, &protocol_version, 0, 0, 0 },
670 {"server", 0, POPT_ARG_NONE, &am_server, 0, 0, 0 },
671 {"temp-dir", 'T', POPT_ARG_STRING, &tmpdir, 0, 0, 0 },
672 {"verbose", 'v', POPT_ARG_NONE, 0, 'v', 0, 0 },
673 {"no-verbose", 0, POPT_ARG_VAL, &verbose, 0, 0, 0 },
674 {"no-v", 0, POPT_ARG_VAL, &verbose, 0, 0, 0 },
675 {"help", 'h', POPT_ARG_NONE, 0, 'h', 0, 0 },
680 static char err_buf[200];
684 * Store the option error message, if any, so that we can log the
685 * connection attempt (which requires parsing the options), and then
686 * show the error later on.
688 void option_error(void)
691 strlcpy(err_buf, "Error parsing options: option may "
692 "be supported on client but not on server?\n",
696 rprintf(FERROR, RSYNC_NAME ": %s", err_buf);
702 * Tweak the option table to disable all options that the rsyncd.conf
703 * file has told us to refuse.
705 static void set_refuse_options(char *bp)
707 struct poptOption *op;
708 char *cp, shortname[2];
709 int is_wild, found_match;
714 while (*bp == ' ') bp++;
717 if ((cp = strchr(bp, ' ')) != NULL)
719 is_wild = strpbrk(bp, "*?[") != NULL;
721 for (op = long_options; ; op++) {
722 *shortname = op->shortName;
723 if (!op->longName && !*shortname)
725 if ((op->longName && wildmatch(bp, op->longName))
726 || (*shortname && wildmatch(bp, shortname))) {
727 if (op->argInfo == POPT_ARG_VAL)
728 op->argInfo = POPT_ARG_NONE;
729 op->val = (op - long_options) + OPT_REFUSED_BASE;
731 /* These flags are set to let us easily check
732 * an implied option later in the code. */
733 switch (*shortname) {
734 case 'r': case 'd': case 'l': case 'p':
735 case 't': case 'g': case 'o': case 'D':
736 refused_archive_part = op->val;
739 refused_compress = op->val;
742 if (wildmatch("delete", op->longName))
743 refused_delete = op->val;
744 else if (wildmatch("delete-before", op->longName))
745 refused_delete_before = op->val;
746 else if (wildmatch("delete-during", op->longName))
747 refused_delete_during = op->val;
748 else if (wildmatch("partial", op->longName))
749 refused_partial = op->val;
750 else if (wildmatch("progress", op->longName))
751 refused_progress = op->val;
752 else if (wildmatch("inplace", op->longName))
753 refused_inplace = op->val;
761 rprintf(FLOG, "No match for refuse-options string \"%s\"\n",
772 static int count_args(const char **argv)
777 while (argv[i] != NULL)
785 static OFF_T parse_size_arg(char **size_arg, char def_suf)
787 int reps, mult, make_compatible = 0;
791 for (arg = *size_arg; isDigit(arg); arg++) {}
793 for (arg++; isDigit(arg); arg++) {}
794 switch (*arg && *arg != '+' && *arg != '-' ? *arg++ : def_suf) {
810 if (*arg == 'b' || *arg == 'B')
811 mult = 1000, make_compatible = 1, arg++;
812 else if (!*arg || *arg == '+' || *arg == '-')
814 else if (strncasecmp(arg, "ib", 2) == 0)
815 mult = 1024, arg += 2;
820 size *= atof(*size_arg);
821 if ((*arg == '+' || *arg == '-') && arg[1] == '1')
822 size += atoi(arg), make_compatible = 1, arg += 2;
825 if (size > 0 && make_compatible) {
826 /* We convert this manually because we may need %lld precision,
827 * and that's not a portable sprintf() escape. */
828 char buf[128], *s = buf + sizeof buf - 1;
832 *--s = (char)(num % 10) + '0';
835 if (!(*size_arg = strdup(s)))
836 out_of_memory("parse_size_arg");
842 static void create_refuse_error(int which)
844 /* The "which" value is the index + OPT_REFUSED_BASE. */
845 struct poptOption *op = &long_options[which - OPT_REFUSED_BASE];
846 int n = snprintf(err_buf, sizeof err_buf,
847 "The server is configured to refuse --%s\n",
850 snprintf(err_buf + n, sizeof err_buf - n,
851 " (-%c)\n", op->shortName);
857 * Process command line arguments. Called on both local and remote.
859 * @retval 1 if all options are OK; with globals set to appropriate
862 * @retval 0 on error, with err_buf containing an explanation
864 int parse_arguments(int *argc_p, const char ***argv_p, int frommain)
866 static poptContext pc;
867 char *ref = lp_refuse_options(module_id);
868 const char *arg, **argv = *argv_p;
873 set_refuse_options(ref);
875 set_refuse_options("log-file*");
878 if (!am_daemon && (arg = getenv("RSYNC_ICONV")) != NULL && *arg)
879 iconv_opt = strdup(arg);
882 /* TODO: Call poptReadDefaultConfig; handle errors. */
884 /* The context leaks in case of an error, but if there's a
885 * problem we always exit anyhow. */
888 pc = poptGetContext(RSYNC_NAME, argc, argv, long_options, 0);
890 poptReadDefaultConfig(pc, 0);
892 while ((opt = poptGetNextOpt(pc)) != -1) {
893 /* most options are handled automatically by popt;
894 * only special cases are returned and listed here. */
898 print_rsync_version(FINFO);
903 /* Disable popt aliases on the server side and
904 * then start parsing the options again. */
906 pc = poptGetContext(RSYNC_NAME, argc, argv,
918 exit_cleanup(RERR_SYNTAX);
926 "Attempt to hack rsync thwarted!\n",
934 pc = poptGetContext(RSYNC_NAME, argc, argv,
935 long_daemon_options, 0);
936 while ((opt = poptGetNextOpt(pc)) != -1) {
948 "rsync: %s: %s (in daemon mode)\n",
949 poptBadOption(pc, POPT_BADOPTION_NOALIAS),
955 if (tmpdir && strlen(tmpdir) >= MAXPATHLEN - 10) {
956 snprintf(err_buf, sizeof err_buf,
957 "the --temp-dir path is WAY too long.\n");
962 rprintf(FERROR, "Daemon option(s) used without --daemon.\n");
965 "(Type \"rsync --daemon --help\" for assistance with daemon mode.)\n");
966 exit_cleanup(RERR_SYNTAX);
969 *argv_p = argv = poptGetArgs(pc);
970 *argc_p = argc = count_args(argv);
976 case OPT_MODIFY_WINDOW:
977 /* The value has already been set by popt, but
978 * we need to remember that we're using a
979 * non-default setting. */
980 modify_window_set = 1;
984 parse_rule(&filter_list, poptGetOptArg(pc), 0, 0);
988 parse_rule(&filter_list, poptGetOptArg(pc),
989 0, XFLG_OLD_PREFIXES);
993 parse_rule(&filter_list, poptGetOptArg(pc),
994 MATCHFLG_INCLUDE, XFLG_OLD_PREFIXES);
997 case OPT_EXCLUDE_FROM:
998 case OPT_INCLUDE_FROM:
999 arg = poptGetOptArg(pc);
1001 arg = sanitize_path(NULL, arg, NULL, 0, NULL);
1002 if (server_filter_list.head) {
1003 char *cp = strdup(arg);
1005 out_of_memory("parse_arguments");
1007 goto options_rejected;
1009 if (check_filter(&server_filter_list, cp, 0) < 0)
1010 goto options_rejected;
1013 parse_filter_file(&filter_list, arg,
1014 opt == OPT_INCLUDE_FROM ? MATCHFLG_INCLUDE : 0,
1015 XFLG_FATAL_ERRORS | XFLG_OLD_PREFIXES);
1019 if (refused_archive_part) {
1020 create_refuse_error(refused_archive_part);
1023 if (!recurse) /* preserve recurse == 2 */
1025 #ifdef SUPPORT_LINKS
1032 preserve_devices = 1;
1033 preserve_specials = 1;
1037 preserve_devices = preserve_specials = 1;
1041 preserve_devices = preserve_specials = 0;
1049 preserve_hard_links++;
1070 switch (++F_option_cnt) {
1072 parse_rule(&filter_list,": /.rsync-filter",0,0);
1075 parse_rule(&filter_list,"- .rsync-filter",0,0);
1081 if (refused_partial || refused_progress) {
1082 create_refuse_error(refused_partial
1083 ? refused_partial : refused_progress);
1091 if (def_compress_level < Z_DEFAULT_COMPRESSION
1092 || def_compress_level > Z_BEST_COMPRESSION) {
1093 snprintf(err_buf, sizeof err_buf,
1094 "--compress-level value is invalid: %d\n",
1095 def_compress_level);
1098 do_compression = def_compress_level != Z_NO_COMPRESSION;
1099 if (do_compression && refused_compress) {
1100 create_refuse_error(refused_compress);
1105 case OPT_WRITE_BATCH:
1106 /* batch_name is already set */
1110 case OPT_ONLY_WRITE_BATCH:
1111 /* batch_name is already set */
1115 case OPT_READ_BATCH:
1116 /* batch_name is already set */
1121 if ((max_size = parse_size_arg(&max_size_arg, 'b')) <= 0) {
1122 snprintf(err_buf, sizeof err_buf,
1123 "--max-size value is invalid: %s\n",
1130 if ((min_size = parse_size_arg(&min_size_arg, 'b')) <= 0) {
1131 snprintf(err_buf, sizeof err_buf,
1132 "--min-size value is invalid: %s\n",
1146 #ifdef SUPPORT_HARD_LINKS
1148 dest_option = "--link-dest";
1151 snprintf(err_buf, sizeof err_buf,
1152 "hard links are not supported on this %s\n",
1153 am_server ? "server" : "client");
1159 dest_option = "--copy-dest";
1162 case OPT_COMPARE_DEST:
1164 dest_option = "--compare-dest";
1166 if (basis_dir_cnt >= MAX_BASIS_DIRS) {
1167 snprintf(err_buf, sizeof err_buf,
1168 "ERROR: at most %d %s args may be specified\n",
1169 MAX_BASIS_DIRS, dest_option);
1172 /* We defer sanitizing this arg until we know what
1173 * our destination directory is going to be. */
1174 basis_dir[basis_dir_cnt++] = (char *)poptGetOptArg(pc);
1178 arg = poptGetOptArg(pc);
1179 if (!parse_chmod(arg, &chmod_modes)) {
1180 snprintf(err_buf, sizeof err_buf,
1181 "Invalid argument passed to --chmod (%s)\n",
1197 /* FIXME: this should probably be ignored with a
1198 * warning and then countermeasures taken to
1199 * restrict group and other access in the presence
1200 * of any more restrictive ACLs, but this is safe
1202 snprintf(err_buf,sizeof(err_buf),
1203 "ACLs are not supported on this %s\n",
1204 am_server ? "server" : "client");
1209 #ifdef SUPPORT_XATTRS
1213 snprintf(err_buf,sizeof(err_buf),
1214 "extended attributes are not supported on this %s\n",
1215 am_server ? "server" : "client");
1220 /* A large opt value means that set_refuse_options()
1221 * turned this option off. */
1222 if (opt >= OPT_REFUSED_BASE) {
1223 create_refuse_error(opt);
1226 snprintf(err_buf, sizeof err_buf, "%s%s: %s\n",
1227 am_server ? "on remote machine: " : "",
1228 poptBadOption(pc, POPT_BADOPTION_NOALIAS),
1234 if (human_readable && argc == 2) {
1235 /* Allow the old meaning of 'h' (--help) on its own. */
1249 if (!am_server && strcmp(iconv_opt, "-") == 0)
1252 need_unsorted_flist = 1;
1256 #ifndef SUPPORT_LINKS
1257 if (preserve_links && !am_sender) {
1258 snprintf(err_buf, sizeof err_buf,
1259 "symlinks are not supported on this %s\n",
1260 am_server ? "server" : "client");
1265 #ifndef SUPPORT_HARD_LINKS
1266 if (preserve_hard_links) {
1267 snprintf(err_buf, sizeof err_buf,
1268 "hard links are not supported on this %s\n",
1269 am_server ? "server" : "client");
1274 #ifndef SUPPORT_XATTRS
1276 snprintf(err_buf, sizeof err_buf,
1277 "--fake-super requires an rsync with extended attributes enabled\n");
1282 if (write_batch && read_batch) {
1283 snprintf(err_buf, sizeof err_buf,
1284 "--write-batch and --read-batch can not be used together\n");
1287 if (write_batch > 0 || read_batch) {
1290 "ignoring --%s-batch option sent to server\n",
1291 write_batch ? "write" : "read");
1292 /* We don't actually exit_cleanup(), so that we can
1293 * still service older version clients that still send
1294 * batch args to server. */
1295 read_batch = write_batch = 0;
1300 if (read_batch && files_from) {
1301 snprintf(err_buf, sizeof err_buf,
1302 "--read-batch cannot be used with --files-from\n");
1305 if (batch_name && strlen(batch_name) > MAX_BATCH_NAME_LEN) {
1306 snprintf(err_buf, sizeof err_buf,
1307 "the batch-file name must be %d characters or less.\n",
1308 MAX_BATCH_NAME_LEN);
1312 if (tmpdir && strlen(tmpdir) >= MAXPATHLEN - 10) {
1313 snprintf(err_buf, sizeof err_buf,
1314 "the --temp-dir path is WAY too long.\n");
1318 if (max_delete < 0 && max_delete != INT_MIN) {
1319 /* Negative numbers are treated as "no deletions". */
1323 if (compare_dest + copy_dest + link_dest > 1) {
1324 snprintf(err_buf, sizeof err_buf,
1325 "You may not mix --compare-dest, --copy-dest, and --link-dest.\n");
1330 if (recurse == 1) /* preserve recurse == 2 */
1337 xfer_dirs = recurse || list_only;
1339 if (relative_paths < 0)
1340 relative_paths = files_from? 1 : 0;
1341 if (!relative_paths)
1344 if (delete_before + !!delete_during + delete_after > 1) {
1345 snprintf(err_buf, sizeof err_buf,
1346 "You may not combine multiple --delete-WHEN options.\n");
1349 if (delete_before || delete_during || delete_after)
1351 else if (delete_mode || delete_excluded) {
1352 /* Only choose now between before & during if one is refused. */
1353 if (refused_delete_before) {
1354 if (!refused_delete_during)
1357 create_refuse_error(refused_delete_before);
1360 } else if (refused_delete_during)
1364 if (!xfer_dirs && delete_mode) {
1365 snprintf(err_buf, sizeof err_buf,
1366 "--delete does not work without -r or -d.\n");
1370 if (delete_mode && refused_delete) {
1371 create_refuse_error(refused_delete);
1375 if (remove_source_files) {
1376 /* We only want to infer this refusal of --remove-source-files
1377 * via the refusal of "delete", not any of the "delete-FOO"
1379 if (refused_delete && am_sender) {
1380 create_refuse_error(refused_delete);
1383 need_messages_from_generator = 1;
1386 *argv_p = argv = poptGetArgs(pc);
1387 *argc_p = argc = count_args(argv);
1389 if (sanitize_paths) {
1391 for (i = argc; i-- > 0; )
1392 argv[i] = sanitize_path(NULL, argv[i], "", 0, NULL);
1394 tmpdir = sanitize_path(NULL, tmpdir, NULL, 0, NULL);
1396 backup_dir = sanitize_path(NULL, backup_dir, NULL, 0, NULL);
1398 if (server_filter_list.head && !am_sender) {
1399 struct filter_list_struct *elp = &server_filter_list;
1402 goto options_rejected;
1403 clean_fname(tmpdir, 1);
1404 if (check_filter(elp, tmpdir, 1) < 0)
1405 goto options_rejected;
1409 goto options_rejected;
1410 clean_fname(backup_dir, 1);
1411 if (check_filter(elp, backup_dir, 1) < 0)
1412 goto options_rejected;
1417 backup_suffix = backup_dir ? "" : BACKUP_SUFFIX;
1418 backup_suffix_len = strlen(backup_suffix);
1419 if (strchr(backup_suffix, '/') != NULL) {
1420 snprintf(err_buf, sizeof err_buf,
1421 "--suffix cannot contain slashes: %s\n",
1426 backup_dir_len = strlcpy(backup_dir_buf, backup_dir, sizeof backup_dir_buf);
1427 backup_dir_remainder = sizeof backup_dir_buf - backup_dir_len;
1428 if (backup_dir_remainder < 32) {
1429 snprintf(err_buf, sizeof err_buf,
1430 "the --backup-dir path is WAY too long.\n");
1433 if (backup_dir_buf[backup_dir_len - 1] != '/') {
1434 backup_dir_buf[backup_dir_len++] = '/';
1435 backup_dir_buf[backup_dir_len] = '\0';
1437 if (verbose > 1 && !am_sender)
1438 rprintf(FINFO, "backup_dir is %s\n", backup_dir_buf);
1439 } else if (!backup_suffix_len && (!am_server || !am_sender)) {
1440 snprintf(err_buf, sizeof err_buf,
1441 "--suffix cannot be a null string without --backup-dir\n");
1443 } else if (make_backups && delete_mode && !delete_excluded && !am_server) {
1444 snprintf(backup_dir_buf, sizeof backup_dir_buf,
1445 "P *%s", backup_suffix);
1446 parse_rule(&filter_list, backup_dir_buf, 0, 0);
1449 if (make_backups && !backup_dir) {
1450 omit_dir_times = 0; /* Implied, so avoid -O to sender. */
1451 if (preserve_times > 1)
1453 } else if (omit_dir_times) {
1454 if (preserve_times > 1)
1458 if (stdout_format) {
1459 if (am_server && log_format_has(stdout_format, 'I'))
1460 stdout_format_has_i = 2;
1461 else if (log_format_has(stdout_format, 'i'))
1462 stdout_format_has_i = itemize_changes | 1;
1463 if (!log_format_has(stdout_format, 'b')
1464 && !log_format_has(stdout_format, 'c'))
1465 log_before_transfer = !am_server;
1466 } else if (itemize_changes) {
1467 stdout_format = "%i %n%L";
1468 stdout_format_has_i = itemize_changes;
1469 log_before_transfer = !am_server;
1472 if (do_progress && !verbose && !log_before_transfer && !am_server)
1478 set_io_timeout(io_timeout);
1480 if (verbose && !stdout_format) {
1481 stdout_format = "%n%L";
1482 log_before_transfer = !am_server;
1484 if (stdout_format_has_i || log_format_has(stdout_format, 'o'))
1485 stdout_format_has_o_or_i = 1;
1487 if (logfile_name && !am_daemon) {
1488 if (!logfile_format) {
1489 logfile_format = "%i %n%L";
1490 logfile_format_has_i = logfile_format_has_o_or_i = 1;
1492 if (log_format_has(logfile_format, 'i'))
1493 logfile_format_has_i = 1;
1494 if (logfile_format_has_i || log_format_has(logfile_format, 'o'))
1495 logfile_format_has_o_or_i = 1;
1498 } else if (!am_daemon)
1499 logfile_format = NULL;
1501 if (daemon_bwlimit && (!bwlimit || bwlimit > daemon_bwlimit))
1502 bwlimit = daemon_bwlimit;
1504 bwlimit_writemax = (size_t)bwlimit * 128;
1505 if (bwlimit_writemax < 512)
1506 bwlimit_writemax = 512;
1509 if (sparse_files && inplace) {
1510 /* Note: we don't check for this below, because --append is
1511 * OK with --sparse (as long as redos are handled right). */
1512 snprintf(err_buf, sizeof err_buf,
1513 "--sparse cannot be used with --inplace\n");
1518 if (whole_file > 0) {
1519 snprintf(err_buf, sizeof err_buf,
1520 "--append cannot be used with --whole-file\n");
1523 if (refused_inplace) {
1524 create_refuse_error(refused_inplace);
1530 if (delay_updates && !partial_dir)
1531 partial_dir = tmp_partialdir;
1534 #ifdef HAVE_FTRUNCATE
1536 snprintf(err_buf, sizeof err_buf,
1537 "--%s cannot be used with --%s\n",
1538 append_mode ? "append" : "inplace",
1539 delay_updates ? "delay-updates" : "partial-dir");
1542 /* --inplace implies --partial for refusal purposes, but we
1543 * clear the keep_partial flag for internal logic purposes. */
1544 if (refused_partial) {
1545 create_refuse_error(refused_partial);
1550 snprintf(err_buf, sizeof err_buf,
1551 "--%s is not supported on this %s\n",
1552 append_mode ? "append" : "inplace",
1553 am_server ? "server" : "client");
1557 if (keep_partial && !partial_dir && !am_server) {
1558 if ((arg = getenv("RSYNC_PARTIAL_DIR")) != NULL && *arg)
1559 partial_dir = strdup(arg);
1563 clean_fname(partial_dir, 1);
1564 if (!*partial_dir || strcmp(partial_dir, ".") == 0)
1566 if (!partial_dir && refused_partial) {
1567 create_refuse_error(refused_partial);
1577 if (argc > 2 || (!am_daemon && argc == 1)) {
1579 exit_cleanup(RERR_SYNTAX);
1581 if (strcmp(files_from, "-") == 0) {
1584 filesfrom_host = ""; /* reading from socket */
1585 } else if ((p = check_for_hostspec(files_from, &h, &q)) != 0) {
1587 snprintf(err_buf, sizeof err_buf,
1588 "The --files-from sent to the server cannot specify a host.\n");
1593 if (strcmp(files_from, "-") == 0) {
1594 snprintf(err_buf, sizeof err_buf,
1595 "Invalid --files-from remote filename\n");
1600 files_from = sanitize_path(NULL, files_from, NULL, 0, NULL);
1601 if (server_filter_list.head) {
1603 goto options_rejected;
1604 clean_fname(files_from, 1);
1605 if (check_filter(&server_filter_list, files_from, 0) < 0)
1606 goto options_rejected;
1608 filesfrom_fd = open(files_from, O_RDONLY|O_BINARY);
1609 if (filesfrom_fd < 0) {
1610 snprintf(err_buf, sizeof err_buf,
1611 "failed to open files-from file %s: %s\n",
1612 files_from, strerror(errno));
1623 snprintf(err_buf, sizeof err_buf,
1624 "Your options have been rejected by the server.\n");
1630 * Construct a filtered list of options to pass through from the
1631 * client to the server.
1633 * This involves setting options that will tell the server how to
1634 * behave, and also filtering out options that are processed only
1637 void server_options(char **args, int *argc_p)
1639 static char argstr[64];
1645 if (blocking_io == -1)
1648 /* This should always remain first on the server's command-line. */
1649 args[ac++] = "--server";
1651 if (daemon_over_rsh > 0) {
1652 args[ac++] = "--daemon";
1654 /* if we're passing --daemon, we're done */
1659 args[ac++] = "--sender";
1663 for (i = 0; i < verbose; i++)
1666 /* the -q option is intentionally left out */
1671 if (!do_xfers) /* Note: NOT "dry_run"! */
1675 if (xfer_dirs > (recurse || !delete_mode || !am_sender ? 1 : 0))
1680 if (prune_empty_dirs)
1693 /* We don't need to send --no-whole-file, because it's the
1694 * default for remote transfers, and in any case old versions
1695 * of rsync will not understand it. */
1697 if (preserve_hard_links) {
1699 if (preserve_hard_links > 1)
1706 if (preserve_devices) /* ignore preserve_specials here */
1712 else if (preserve_executability && am_sender)
1718 #ifdef SUPPORT_XATTRS
1719 if (preserve_xattrs) {
1721 if (preserve_xattrs > 1)
1727 if (always_checksum)
1735 if (one_file_system) {
1737 if (one_file_system > 1)
1745 /* This is a complete hack - blame Rusty. FIXME!
1746 * This hack is only needed for older rsync versions that
1747 * don't understand the --list-only option. */
1748 if (list_only == 1 && !recurse)
1751 #if SUBPROTOCOL_VERSION != 0
1752 /* If we're speaking a pre-release version of a protocol, we tell
1753 * the server about this by (ab)using the -e option. */
1754 if (protocol_version == PROTOCOL_VERSION) {
1755 x += snprintf(argstr+x, sizeof argstr - x,
1756 "e%d.%d", PROTOCOL_VERSION, SUBPROTOCOL_VERSION);
1763 args[ac++] = argstr;
1766 args[ac++] = "--list-only";
1768 /* This makes sure that the remote rsync can handle deleting with -d
1769 * sans -r because the --no-r option was added at the same time. */
1770 if (xfer_dirs && !recurse && delete_mode && am_sender)
1771 args[ac++] = "--no-r";
1773 if (do_compression && def_compress_level != Z_DEFAULT_COMPRESSION) {
1774 if (asprintf(&arg, "--compress-level=%d", def_compress_level) < 0)
1779 if (preserve_devices) {
1780 /* Note: sending "--devices" would not be backward-compatible. */
1781 if (!preserve_specials)
1782 args[ac++] = "--no-specials"; /* -D is already set. */
1783 } else if (preserve_specials)
1784 args[ac++] = "--specials";
1786 /* The server side doesn't use our log-format, but in certain
1787 * circumstances they need to know a little about the option. */
1788 if (stdout_format && am_sender) {
1789 /* Use --log-format, not --out-format, for compatibility. */
1790 if (stdout_format_has_i > 1)
1791 args[ac++] = "--log-format=%i%I";
1792 else if (stdout_format_has_i)
1793 args[ac++] = "--log-format=%i";
1794 else if (stdout_format_has_o_or_i)
1795 args[ac++] = "--log-format=%o";
1797 args[ac++] = "--log-format=X";
1802 char *set = strchr(iconv_opt, ',');
1807 if (asprintf(&arg, "--iconv=%s", set) < 0)
1814 if (asprintf(&arg, "-B%lu", block_size) < 0)
1820 if (asprintf(&arg, "--timeout=%d", io_timeout) < 0)
1826 if (asprintf(&arg, "--bwlimit=%d", bwlimit) < 0)
1832 args[ac++] = "--backup-dir";
1833 args[ac++] = backup_dir;
1836 /* Only send --suffix if it specifies a non-default value. */
1837 if (strcmp(backup_suffix, backup_dir ? "" : BACKUP_SUFFIX) != 0) {
1838 /* We use the following syntax to avoid weirdness with '~'. */
1839 if (asprintf(&arg, "--suffix=%s", backup_suffix) < 0)
1845 if (max_delete > 0) {
1846 if (asprintf(&arg, "--max-delete=%d", max_delete) < 0)
1849 } else if (max_delete == 0)
1850 args[ac++] = "--max_delete=-1";
1852 args[ac++] = "--min-size";
1853 args[ac++] = min_size_arg;
1856 args[ac++] = "--max-size";
1857 args[ac++] = max_size_arg;
1860 args[ac++] = "--delete-before";
1861 else if (delete_during == 2)
1862 args[ac++] = "--delete-delay";
1863 else if (delete_during)
1864 args[ac++] = "--delete-during";
1865 else if (delete_after)
1866 args[ac++] = "--delete-after";
1867 else if (delete_mode && !delete_excluded)
1868 args[ac++] = "--delete";
1869 if (delete_excluded)
1870 args[ac++] = "--delete-excluded";
1872 args[ac++] = "--force";
1873 if (write_batch < 0)
1874 args[ac++] = "--only-write-batch=X";
1876 args[ac++] = "--super";
1878 args[ac++] = "--size-only";
1880 if (skip_compress) {
1881 if (asprintf(&arg, "--skip-compress=%s", skip_compress) < 0)
1887 if (modify_window_set) {
1888 if (asprintf(&arg, "--modify-window=%d", modify_window) < 0)
1893 if (checksum_seed) {
1894 if (asprintf(&arg, "--checksum-seed=%d", checksum_seed) < 0)
1899 if (partial_dir && am_sender) {
1900 if (partial_dir != tmp_partialdir) {
1901 args[ac++] = "--partial-dir";
1902 args[ac++] = partial_dir;
1905 args[ac++] = "--delay-updates";
1906 } else if (keep_partial && am_sender)
1907 args[ac++] = "--partial";
1910 args[ac++] = "--ignore-errors";
1912 if (copy_unsafe_links)
1913 args[ac++] = "--copy-unsafe-links";
1916 args[ac++] = "--safe-links";
1919 args[ac++] = "--numeric-ids";
1921 if (!allow_inc_recurse)
1922 args[ac++] = "--no-ir";
1925 if (ignore_existing)
1926 args[ac++] = "--ignore-existing";
1928 /* Backward compatibility: send --existing, not --ignore-non-existing. */
1929 if (ignore_non_existing)
1930 args[ac++] = "--existing";
1933 args[ac++] = "--temp-dir";
1934 args[ac++] = tmpdir;
1938 /* the server only needs this option if it is not the sender,
1939 * and it may be an older version that doesn't know this
1940 * option, so don't send it if client is the sender.
1943 for (i = 0; i < basis_dir_cnt; i++) {
1944 args[ac++] = dest_option;
1945 args[ac++] = basis_dir[i];
1951 if (append_mode > 1)
1952 args[ac++] = "--append";
1953 args[ac++] = "--append";
1955 args[ac++] = "--inplace";
1957 if (files_from && (!am_sender || filesfrom_host)) {
1958 if (filesfrom_host) {
1959 args[ac++] = "--files-from";
1960 args[ac++] = files_from;
1962 args[ac++] = "--from0";
1964 args[ac++] = "--files-from=-";
1965 args[ac++] = "--from0";
1967 if (!relative_paths)
1968 args[ac++] = "--no-relative";
1970 if (relative_paths && !implied_dirs && !am_sender)
1971 args[ac++] = "--no-implied-dirs";
1973 if (fuzzy_basis && am_sender)
1974 args[ac++] = "--fuzzy";
1976 if (remove_source_files == 1)
1977 args[ac++] = "--remove-source-files";
1978 else if (remove_source_files)
1979 args[ac++] = "--remove-sent-files";
1985 out_of_memory("server_options");
1988 /* Look for a HOST specfication of the form "HOST:PATH", "HOST::PATH", or
1989 * "rsync://HOST:PORT/PATH". If found, *host_ptr will be set to some allocated
1990 * memory with the HOST. If a daemon-accessing spec was specified, the value
1991 * of *port_ptr will contain a non-0 port number, otherwise it will be set to
1992 * 0. The return value is a pointer to the PATH. Note that the HOST spec can
1993 * be an IPv6 literal address enclosed in '[' and ']' (such as "[::1]" or
1994 * "[::ffff:127.0.0.1]") which is returned without the '[' and ']'. */
1995 char *check_for_hostspec(char *s, char **host_ptr, int *port_ptr)
2001 if (port_ptr && strncasecmp(URL_PREFIX, s, strlen(URL_PREFIX)) == 0) {
2003 s += strlen(URL_PREFIX);
2004 if ((p = strchr(s, '/')) != NULL) {
2008 hostlen = strlen(s);
2011 if (*s == '[' && (p = strchr(s, ']')) != NULL) {
2015 *port_ptr = atoi(p+2);
2017 if ((p = strchr(s, ':')) != NULL && p < s + hostlen) {
2019 *port_ptr = atoi(p+1);
2023 *port_ptr = RSYNC_PORT;
2024 *host_ptr = new_array(char, hostlen + 1);
2025 strlcpy(*host_ptr, s, hostlen + 1);
2029 if (*s == '[' && (p = strchr(s, ']')) != NULL && p[1] == ':') {
2033 not_host = strchr(s, '/') || !strchr(s, ':');
2039 if (!(p = strchr(s, ':')))
2043 not_host = strchr(s, '/') != NULL;
2049 *host_ptr = new_array(char, hostlen + 1);
2050 strlcpy(*host_ptr, s, hostlen + 1);
2053 if (port_ptr && !*port_ptr)
2054 *port_ptr = RSYNC_PORT;