2 Copyright (C) Andrew Tridgell 1998
4 This program is free software; you can redistribute it and/or modify
5 it under the terms of the GNU General Public License as published by
6 the Free Software Foundation; either version 2 of the License, or
7 (at your option) any later version.
9 This program is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 GNU General Public License for more details.
14 You should have received a copy of the GNU General Public License
15 along with this program; if not, write to the Free Software
16 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
19 /* options parsing code */
27 int preserve_links = 0;
28 int preserve_hard_links = 0;
29 int preserve_perms = 0;
30 int preserve_devices = 0;
33 int preserve_times = 0;
40 int delete_excluded=0;
41 int one_file_system=0;
62 int copy_unsafe_links=0;
63 int block_size=BLOCK_SIZE;
77 char *backup_suffix = BACKUP_SUFFIX;
79 char *compare_dest = NULL;
80 char *config_file = RSYNCD_CONF;
81 char *shell_cmd = NULL;
82 char *log_format = NULL;
83 char *password_file = NULL;
84 char *rsync_path = RSYNC_NAME;
85 char *backup_dir = NULL;
86 int rsync_port = RSYNC_PORT;
90 int always_checksum = 0;
93 static int modify_window_set;
96 struct in_addr socket_address = {INADDR_ANY};
98 void usage(enum logcode F)
100 rprintf(F,"rsync version %s Copyright Andrew Tridgell and Paul Mackerras\n\n",
103 rprintf(F,"rsync is a file transfer program capable of efficient remote update\nvia a fast differencing algorithm.\n\n");
105 rprintf(F,"Usage: rsync [OPTION]... SRC [SRC]... [USER@]HOST:DEST\n");
106 rprintf(F," or rsync [OPTION]... [USER@]HOST:SRC DEST\n");
107 rprintf(F," or rsync [OPTION]... SRC [SRC]... DEST\n");
108 rprintf(F," or rsync [OPTION]... [USER@]HOST::SRC [DEST]\n");
109 rprintf(F," or rsync [OPTION]... SRC [SRC]... [USER@]HOST::DEST\n");
110 rprintf(F," or rsync [OPTION]... rsync://[USER@]HOST[:PORT]/SRC [DEST]\n");
111 rprintf(F,"SRC on single-colon remote HOST will be expanded by remote shell\n");
112 rprintf(F,"SRC on server remote HOST may contain shell wildcards or multiple\n");
113 rprintf(F," sources separated by space as long as they have same top-level\n");
114 rprintf(F,"\nOptions\n");
115 rprintf(F," -v, --verbose increase verbosity\n");
116 rprintf(F," -q, --quiet decrease verbosity\n");
117 rprintf(F," -c, --checksum always checksum\n");
118 rprintf(F," -a, --archive archive mode\n");
119 rprintf(F," -r, --recursive recurse into directories\n");
120 rprintf(F," -R, --relative use relative path names\n");
121 rprintf(F," -b, --backup make backups (default %s suffix)\n",BACKUP_SUFFIX);
122 rprintf(F," --backup-dir make backups into this directory\n");
123 rprintf(F," --suffix=SUFFIX override backup suffix\n");
124 rprintf(F," -u, --update update only (don't overwrite newer files)\n");
125 rprintf(F," -l, --links preserve soft links\n");
126 rprintf(F," -L, --copy-links treat soft links like regular files\n");
127 rprintf(F," --copy-unsafe-links copy links outside the source tree\n");
128 rprintf(F," --safe-links ignore links outside the destination tree\n");
129 rprintf(F," -H, --hard-links preserve hard links\n");
130 rprintf(F," -p, --perms preserve permissions\n");
131 rprintf(F," -o, --owner preserve owner (root only)\n");
132 rprintf(F," -g, --group preserve group\n");
133 rprintf(F," -D, --devices preserve devices (root only)\n");
134 rprintf(F," -t, --times preserve times\n");
135 rprintf(F," -S, --sparse handle sparse files efficiently\n");
136 rprintf(F," -n, --dry-run show what would have been transferred\n");
137 rprintf(F," -W, --whole-file copy whole files, no incremental checks\n");
138 rprintf(F," -x, --one-file-system don't cross filesystem boundaries\n");
139 rprintf(F," -B, --block-size=SIZE checksum blocking size (default %d)\n",BLOCK_SIZE);
140 rprintf(F," -e, --rsh=COMMAND specify rsh replacement\n");
141 rprintf(F," --rsync-path=PATH specify path to rsync on the remote machine\n");
142 rprintf(F," -C, --cvs-exclude auto ignore files in the same way CVS does\n");
143 rprintf(F," --existing only update files that already exist\n");
144 rprintf(F," --delete delete files that don't exist on the sending side\n");
145 rprintf(F," --delete-excluded also delete excluded files on the receiving side\n");
146 rprintf(F," --delete-after delete after transferring, not before\n");
147 rprintf(F," --ignore-errors delete even if there are IO errors\n");
148 rprintf(F," --max-delete=NUM don't delete more than NUM files\n");
149 rprintf(F," --partial keep partially transferred files\n");
150 rprintf(F," --force force deletion of directories even if not empty\n");
151 rprintf(F," --numeric-ids don't map uid/gid values by user/group name\n");
152 rprintf(F," --timeout=TIME set IO timeout in seconds\n");
153 rprintf(F," -I, --ignore-times don't exclude files that match length and time\n");
154 rprintf(F," --size-only only use file size when determining if a file should be transferred\n");
155 rprintf(F," --modify-window=NUM Timestamp window (seconds) for file match (default=%d)\n",modify_window);
156 rprintf(F," -T --temp-dir=DIR create temporary files in directory DIR\n");
157 rprintf(F," --compare-dest=DIR also compare destination files relative to DIR\n");
158 rprintf(F," -P equivalent to --partial --progress\n");
159 rprintf(F," -z, --compress compress file data\n");
160 rprintf(F," --exclude=PATTERN exclude files matching PATTERN\n");
161 rprintf(F," --exclude-from=FILE exclude patterns listed in FILE\n");
162 rprintf(F," --include=PATTERN don't exclude files matching PATTERN\n");
163 rprintf(F," --include-from=FILE don't exclude patterns listed in FILE\n");
164 rprintf(F," --version print version number\n");
165 rprintf(F," --daemon run as a rsync daemon\n");
166 rprintf(F," --address bind to the specified address\n");
167 rprintf(F," --config=FILE specify alternate rsyncd.conf file\n");
168 rprintf(F," --port=PORT specify alternate rsyncd port number\n");
169 rprintf(F," --blocking-io use blocking IO for the remote shell\n");
170 rprintf(F," --stats give some file transfer stats\n");
171 rprintf(F," --progress show progress during transfer\n");
172 rprintf(F," --log-format=FORMAT log file transfers using specified format\n");
173 rprintf(F," --password-file=FILE get password from FILE\n");
174 rprintf(F," --bwlimit=KBPS limit I/O bandwidth, KBytes per second\n");
175 rprintf(F," -h, --help show this help screen\n");
179 rprintf(F,"\nPlease see the rsync(1) and rsyncd.conf(5) man pages for full documentation\n");
180 rprintf(F,"See http://rsync.samba.org/ for updates and bug reports\n");
183 enum {OPT_VERSION, OPT_SUFFIX, OPT_SENDER, OPT_SERVER, OPT_EXCLUDE,
184 OPT_EXCLUDE_FROM, OPT_DELETE, OPT_DELETE_EXCLUDED, OPT_NUMERIC_IDS,
185 OPT_RSYNC_PATH, OPT_FORCE, OPT_TIMEOUT, OPT_DAEMON, OPT_CONFIG, OPT_PORT,
186 OPT_INCLUDE, OPT_INCLUDE_FROM, OPT_STATS, OPT_PARTIAL, OPT_PROGRESS,
187 OPT_COPY_UNSAFE_LINKS, OPT_SAFE_LINKS, OPT_COMPARE_DEST,
188 OPT_LOG_FORMAT, OPT_PASSWORD_FILE, OPT_SIZE_ONLY, OPT_ADDRESS,
189 OPT_DELETE_AFTER, OPT_EXISTING, OPT_MAX_DELETE, OPT_BACKUP_DIR,
190 OPT_IGNORE_ERRORS, OPT_BWLIMIT, OPT_BLOCKING_IO,
193 static char *short_options = "oblLWHpguDCtcahvqrRIxnSe:B:T:zP";
195 static struct option long_options[] = {
196 {"version", 0, 0, OPT_VERSION},
197 {"server", 0, 0, OPT_SERVER},
198 {"sender", 0, 0, OPT_SENDER},
199 {"existing", 0, 0, OPT_EXISTING},
200 {"delete", 0, 0, OPT_DELETE},
201 {"delete-excluded", 0, 0, OPT_DELETE_EXCLUDED},
202 {"force", 0, 0, OPT_FORCE},
203 {"numeric-ids", 0, 0, OPT_NUMERIC_IDS},
204 {"exclude", 1, 0, OPT_EXCLUDE},
205 {"exclude-from",1, 0, OPT_EXCLUDE_FROM},
206 {"include", 1, 0, OPT_INCLUDE},
207 {"include-from",1, 0, OPT_INCLUDE_FROM},
208 {"rsync-path", 1, 0, OPT_RSYNC_PATH},
209 {"password-file", 1, 0, OPT_PASSWORD_FILE},
210 {"one-file-system",0, 0, 'x'},
211 {"ignore-times",0, 0, 'I'},
212 {"size-only", 0, 0, OPT_SIZE_ONLY},
213 {"modify-window",1, 0, OPT_MODIFY_WINDOW},
215 {"dry-run", 0, 0, 'n'},
216 {"sparse", 0, 0, 'S'},
217 {"cvs-exclude", 0, 0, 'C'},
218 {"archive", 0, 0, 'a'},
219 {"checksum", 0, 0, 'c'},
220 {"backup", 0, 0, 'b'},
221 {"update", 0, 0, 'u'},
222 {"verbose", 0, 0, 'v'},
223 {"quiet", 0, 0, 'q'},
224 {"recursive", 0, 0, 'r'},
225 {"relative", 0, 0, 'R'},
226 {"devices", 0, 0, 'D'},
227 {"perms", 0, 0, 'p'},
228 {"links", 0, 0, 'l'},
229 {"copy-links", 0, 0, 'L'},
230 {"copy-unsafe-links", 0, 0, OPT_COPY_UNSAFE_LINKS},
231 {"safe-links", 0, 0, OPT_SAFE_LINKS},
232 {"whole-file", 0, 0, 'W'},
233 {"hard-links", 0, 0, 'H'},
234 {"owner", 0, 0, 'o'},
235 {"group", 0, 0, 'g'},
236 {"times", 0, 0, 't'},
238 {"suffix", 1, 0, OPT_SUFFIX},
239 {"block-size", 1, 0, 'B'},
240 {"timeout", 1, 0, OPT_TIMEOUT},
241 {"temp-dir", 1, 0, 'T'},
242 {"compare-dest", 1, 0, OPT_COMPARE_DEST},
243 {"compress", 0, 0, 'z'},
244 {"daemon", 0, 0, OPT_DAEMON},
245 {"stats", 0, 0, OPT_STATS},
246 {"progress", 0, 0, OPT_PROGRESS},
247 {"partial", 0, 0, OPT_PARTIAL},
248 {"delete-after",0, 0, OPT_DELETE_AFTER},
249 {"ignore-errors",0, 0, OPT_IGNORE_ERRORS},
250 {"blocking-io" ,0, 0, OPT_BLOCKING_IO},
251 {"config", 1, 0, OPT_CONFIG},
252 {"port", 1, 0, OPT_PORT},
253 {"log-format", 1, 0, OPT_LOG_FORMAT},
254 {"bwlimit", 1, 0, OPT_BWLIMIT},
255 {"address", 1, 0, OPT_ADDRESS},
256 {"max-delete", 1, 0, OPT_MAX_DELETE},
257 {"backup-dir", 1, 0, OPT_BACKUP_DIR},
261 static char err_buf[100];
263 void option_error(void)
266 rprintf(FLOG,"%s", err_buf);
267 rprintf(FERROR,"%s", err_buf);
269 rprintf(FLOG,"Error parsing options - unsupported option?\n");
270 rprintf(FERROR,"Error parsing options - unsupported option?\n");
272 exit_cleanup(RERR_UNSUPPORTED);
275 /* check to see if we should refuse this option */
276 static int check_refuse_options(char *ref, int opt)
282 for (i=0; long_options[i].name; i++) {
283 if (long_options[i].val == opt) break;
286 if (!long_options[i].name) return 0;
288 name = long_options[i].name;
291 while ((p = strstr(ref,name))) {
292 if ((p==ref || p[-1]==' ') &&
293 (p[len] == ' ' || p[len] == 0)) {
294 slprintf(err_buf,sizeof(err_buf),
295 "The '%s' option is not supported by this server\n", name);
304 int parse_arguments(int argc, char *argv[], int frommain)
308 char *ref = lp_refuse_options(module_id);
310 while ((opt = getopt_long(argc, argv,
311 short_options, long_options, &option_index))
315 if (check_refuse_options(ref, opt)) return 0;
320 rprintf(FINFO,"rsync version %s protocol version %d\n\n",
321 VERSION,PROTOCOL_VERSION);
322 rprintf(FINFO,"Written by Andrew Tridgell and Paul Mackerras\n");
326 backup_suffix = optarg;
333 case OPT_PASSWORD_FILE:
334 password_file =optarg;
345 case OPT_MODIFY_WINDOW:
346 modify_window = atoi(optarg);
347 modify_window_set = 1;
362 case OPT_DELETE_AFTER:
366 case OPT_DELETE_EXCLUDED:
375 case OPT_NUMERIC_IDS:
380 add_exclude(optarg, 0);
384 add_exclude(optarg, 1);
387 case OPT_EXCLUDE_FROM:
388 add_exclude_file(optarg,1, 0);
391 case OPT_INCLUDE_FROM:
392 add_exclude_file(optarg,1, 1);
395 case OPT_COPY_UNSAFE_LINKS:
440 #if SUPPORT_HARD_LINKS
441 preserve_hard_links=1;
443 slprintf(err_buf,sizeof(err_buf),"hard links are not supported on this server\n");
444 rprintf(FERROR,"ERROR: hard links not supported on this platform\n");
478 if (frommain) quiet++;
500 exit_cleanup(RERR_SYNTAX);
518 block_size = atoi(optarg);
522 max_delete = atoi(optarg);
526 io_timeout = atoi(optarg);
533 case OPT_COMPARE_DEST:
534 compare_dest = optarg;
557 case OPT_IGNORE_ERRORS:
561 case OPT_BLOCKING_IO:
571 config_file = optarg;
575 rsync_port = atoi(optarg);
583 bwlimit = atoi(optarg);
589 if ((ia = ip_address(optarg))) {
590 socket_address = *ia;
600 slprintf(err_buf,sizeof(err_buf),"unrecognised option\n");
608 /* need to pass all the valid options from the client to the server */
610 void server_options(char **args,int *argc)
613 static char argstr[50];
614 static char bsize[30];
615 static char iotime[30];
616 static char mdelete[30];
617 static char mwindow[30];
622 args[ac++] = "--server";
625 args[ac++] = "--sender";
629 for (i=0;i<verbose;i++)
632 /* the -q option is intentionally left out */
645 if (preserve_hard_links)
651 if (preserve_devices)
674 /* this is a complete hack - blame Rusty
676 this is a hack to make the list_only (remote file list)
678 if (list_only && !recurse)
683 if (x != 1) args[ac++] = argstr;
685 if (block_size != BLOCK_SIZE) {
686 slprintf(bsize,sizeof(bsize),"-B%d",block_size);
690 if (max_delete && am_sender) {
691 slprintf(mdelete,sizeof(mdelete),"--max-delete=%d",max_delete);
692 args[ac++] = mdelete;
696 slprintf(iotime,sizeof(iotime),"--timeout=%d",io_timeout);
701 slprintf(bw,sizeof(bw),"--bwlimit=%d",bwlimit);
705 if (strcmp(backup_suffix, BACKUP_SUFFIX)) {
706 args[ac++] = "--suffix";
707 args[ac++] = backup_suffix;
710 if (delete_mode && !delete_excluded)
711 args[ac++] = "--delete";
714 args[ac++] = "--delete-excluded";
717 args[ac++] = "--size-only";
719 if (modify_window_set) {
720 slprintf(mwindow,sizeof(mwindow),"--modify-window=%d",
722 args[ac++] = mwindow;
726 args[ac++] = "--partial";
729 args[ac++] = "--force";
732 args[ac++] = "--delete-after";
735 args[ac++] = "--ignore-errors";
737 if (copy_unsafe_links)
738 args[ac++] = "--copy-unsafe-links";
741 args[ac++] = "--safe-links";
744 args[ac++] = "--numeric-ids";
746 if (only_existing && am_sender)
747 args[ac++] = "--existing";
750 args[ac++] = "--temp-dir";
754 if (backup_dir && am_sender) {
755 /* only the receiver needs this option, if we are the sender
756 * then we need to send it to the receiver.
758 args[ac++] = "--backup-dir";
759 args[ac++] = backup_dir;
762 if (compare_dest && am_sender) {
763 /* the server only needs this option if it is not the sender,
764 * and it may be an older version that doesn't know this
765 * option, so don't send it if client is the sender.
767 args[ac++] = "--compare-dest";
768 args[ac++] = compare_dest;