Commit | Line | Data |
---|---|---|
7a6421fa AT |
1 | /* |
2 | Copyright (C) Andrew Tridgell 1998 | |
3 | ||
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. | |
8 | ||
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. | |
13 | ||
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. | |
17 | */ | |
18 | ||
19 | /* options parsing code */ | |
20 | ||
21 | #include "rsync.h" | |
22 | ||
23 | ||
24 | int make_backups = 0; | |
25 | int whole_file = 0; | |
26 | int copy_links = 0; | |
27 | int preserve_links = 0; | |
28 | int preserve_hard_links = 0; | |
29 | int preserve_perms = 0; | |
30 | int preserve_devices = 0; | |
31 | int preserve_uid = 0; | |
32 | int preserve_gid = 0; | |
33 | int preserve_times = 0; | |
34 | int update_only = 0; | |
35 | int cvs_exclude = 0; | |
36 | int dry_run=0; | |
37 | int local_server=0; | |
38 | int ignore_times=0; | |
39 | int delete_mode=0; | |
40 | int one_file_system=0; | |
41 | int remote_version=0; | |
42 | int sparse_files=0; | |
43 | int do_compression=0; | |
44 | int am_root=0; | |
45 | int orig_umask=0; | |
46 | int relative_paths=0; | |
47 | int numeric_ids = 0; | |
48 | int force_delete = 0; | |
49 | int io_timeout = 0; | |
50 | int io_error = 0; | |
51 | int read_only = 0; | |
52 | int module_id = -1; | |
53 | int am_server = 0; | |
54 | int am_sender=0; | |
55 | int recurse = 0; | |
56 | int am_daemon=0; | |
8d9dc9f9 | 57 | int am_client=0; |
a800434a | 58 | int do_stats=0; |
c95da96a | 59 | int keep_partial=0; |
7a6421fa AT |
60 | |
61 | int block_size=BLOCK_SIZE; | |
62 | ||
63 | char *backup_suffix = BACKUP_SUFFIX; | |
64 | char *tmpdir = NULL; | |
65 | char *config_file = RSYNCD_CONF; | |
66 | char *shell_cmd = NULL; | |
67 | ||
68 | char *rsync_path = RSYNC_NAME; | |
69 | int rsync_port = RSYNC_PORT; | |
70 | ||
71 | int verbose = 0; | |
72 | int always_checksum = 0; | |
73 | ||
74 | ||
75 | void usage(int F) | |
76 | { | |
77 | rprintf(F,"rsync version %s Copyright Andrew Tridgell and Paul Mackerras\n\n", | |
78 | VERSION); | |
704f908e AT |
79 | |
80 | rprintf(F,"rsync is a file transfer program capable of efficient remote update\nvia a fast differencing algorithm.\n\n"); | |
81 | ||
82 | rprintf(F,"Usage: rsync [OPTION]... SRC [USER@]HOST:DEST\n"); | |
83 | rprintf(F," or rsync [OPTION]... [USER@]HOST:SRC DEST\n"); | |
84 | rprintf(F," or rsync [OPTION]... SRC DEST\n"); | |
85 | rprintf(F," or rsync [OPTION]... [USER@]HOST::SRC DEST\n"); | |
86 | rprintf(F," or rsync [OPTION]... SRC [USER@]HOST::DEST\n"); | |
87 | rprintf(F,"\nOptions\n"); | |
88 | rprintf(F," -v, --verbose increase verbosity\n"); | |
89 | rprintf(F," -c, --checksum always checksum\n"); | |
90 | rprintf(F," -a, --archive archive mode\n"); | |
91 | rprintf(F," -r, --recursive recurse into directories\n"); | |
92 | rprintf(F," -R, --relative use relative path names\n"); | |
93 | rprintf(F," -b, --backup make backups (default ~ extension)\n"); | |
94 | rprintf(F," -u, --update update only (don't overwrite newer files)\n"); | |
95 | rprintf(F," -l, --links preserve soft links\n"); | |
96 | rprintf(F," -L, --copy-links treat soft links like regular files\n"); | |
97 | rprintf(F," -H, --hard-links preserve hard links\n"); | |
98 | rprintf(F," -p, --perms preserve permissions\n"); | |
99 | rprintf(F," -o, --owner preserve owner (root only)\n"); | |
100 | rprintf(F," -g, --group preserve group\n"); | |
101 | rprintf(F," -D, --devices preserve devices (root only)\n"); | |
102 | rprintf(F," -t, --times preserve times\n"); | |
103 | rprintf(F," -S, --sparse handle sparse files efficiently\n"); | |
104 | rprintf(F," -n, --dry-run show what would have been transferred\n"); | |
105 | rprintf(F," -W, --whole-file copy whole files, no incremental checks\n"); | |
106 | rprintf(F," -x, --one-file-system don't cross filesystem boundaries\n"); | |
107 | rprintf(F," -B, --block-size=SIZE checksum blocking size\n"); | |
108 | rprintf(F," -e, --rsh=COMMAND specify rsh replacement\n"); | |
109 | rprintf(F," --rsync-path=PATH specify path to rsync on the remote machine\n"); | |
110 | rprintf(F," -C, --cvs-exclude auto ignore files in the same way CVS does\n"); | |
111 | rprintf(F," --delete delete files that don't exist on the sending side\n"); | |
c95da96a | 112 | rprintf(F," --partial keep partially transferred files\n"); |
704f908e AT |
113 | rprintf(F," --force force deletion of directories even if not empty\n"); |
114 | rprintf(F," --numeric-ids don't map uid/gid values by user/group name\n"); | |
115 | rprintf(F," --timeout=TIME set IO timeout in seconds\n"); | |
116 | rprintf(F," -I, --ignore-times don't exclude files that match length and time\n"); | |
117 | rprintf(F," -T --temp-dir=DIR create temporary files in directory DIR\n"); | |
118 | rprintf(F," -z, --compress compress file data\n"); | |
119 | rprintf(F," --exclude=PATTERN exclude file FILE\n"); | |
120 | rprintf(F," --exclude-from=PATTERN exclude files listed in FILE\n"); | |
121 | rprintf(F," --include=PATTERN don't exclude file FILE\n"); | |
122 | rprintf(F," --include-from=PATTERN don't exclude files listed in FILE\n"); | |
123 | rprintf(F," --suffix=SUFFIX override backup suffix\n"); | |
124 | rprintf(F," --version print version number\n"); | |
125 | rprintf(F," --daemon run as a rsync daemon\n"); | |
126 | rprintf(F," --config=FILE specify alternate rsyncd.conf file\n"); | |
127 | rprintf(F," --port=PORT specify alternate rsyncd port number\n"); | |
128 | rprintf(F," --stats give some file transfer stats\n"); | |
129 | rprintf(F," -h, --help show this help screen\n"); | |
7a6421fa AT |
130 | |
131 | rprintf(F,"\n"); | |
132 | rprintf(F,"the backup suffix defaults to %s\n",BACKUP_SUFFIX); | |
133 | rprintf(F,"the block size defaults to %d\n",BLOCK_SIZE); | |
b72f24c7 AT |
134 | |
135 | rprintf(F,"\nPlease see the rsync(1) and rsyncd.conf(5) man pages for full documentation\n"); | |
136 | rprintf(F,"See http://samba.anu.edu.au/rsync/ for updates and bug reports\n"); | |
7a6421fa AT |
137 | } |
138 | ||
139 | enum {OPT_VERSION,OPT_SUFFIX,OPT_SENDER,OPT_SERVER,OPT_EXCLUDE, | |
140 | OPT_EXCLUDE_FROM,OPT_DELETE,OPT_NUMERIC_IDS,OPT_RSYNC_PATH, | |
2b6b4d53 | 141 | OPT_FORCE,OPT_TIMEOUT,OPT_DAEMON,OPT_CONFIG,OPT_PORT, |
c95da96a | 142 | OPT_INCLUDE, OPT_INCLUDE_FROM, OPT_STATS, OPT_PARTIAL}; |
7a6421fa AT |
143 | |
144 | static char *short_options = "oblLWHpguDCtcahvrRIxnSe:B:T:z"; | |
145 | ||
146 | static struct option long_options[] = { | |
147 | {"version", 0, 0, OPT_VERSION}, | |
148 | {"server", 0, 0, OPT_SERVER}, | |
149 | {"sender", 0, 0, OPT_SENDER}, | |
150 | {"delete", 0, 0, OPT_DELETE}, | |
151 | {"force", 0, 0, OPT_FORCE}, | |
152 | {"numeric-ids", 0, 0, OPT_NUMERIC_IDS}, | |
153 | {"exclude", 1, 0, OPT_EXCLUDE}, | |
154 | {"exclude-from",1, 0, OPT_EXCLUDE_FROM}, | |
2b6b4d53 AT |
155 | {"include", 1, 0, OPT_INCLUDE}, |
156 | {"include-from",1, 0, OPT_INCLUDE_FROM}, | |
7a6421fa AT |
157 | {"rsync-path", 1, 0, OPT_RSYNC_PATH}, |
158 | {"one-file-system",0, 0, 'x'}, | |
159 | {"ignore-times",0, 0, 'I'}, | |
160 | {"help", 0, 0, 'h'}, | |
161 | {"dry-run", 0, 0, 'n'}, | |
162 | {"sparse", 0, 0, 'S'}, | |
163 | {"cvs-exclude", 0, 0, 'C'}, | |
164 | {"archive", 0, 0, 'a'}, | |
165 | {"checksum", 0, 0, 'c'}, | |
166 | {"backup", 0, 0, 'b'}, | |
167 | {"update", 0, 0, 'u'}, | |
168 | {"verbose", 0, 0, 'v'}, | |
169 | {"recursive", 0, 0, 'r'}, | |
170 | {"relative", 0, 0, 'R'}, | |
171 | {"devices", 0, 0, 'D'}, | |
172 | {"perms", 0, 0, 'p'}, | |
173 | {"links", 0, 0, 'l'}, | |
174 | {"copy-links", 0, 0, 'L'}, | |
175 | {"whole-file", 0, 0, 'W'}, | |
176 | {"hard-links", 0, 0, 'H'}, | |
177 | {"owner", 0, 0, 'o'}, | |
178 | {"group", 0, 0, 'g'}, | |
179 | {"times", 0, 0, 't'}, | |
180 | {"rsh", 1, 0, 'e'}, | |
181 | {"suffix", 1, 0, OPT_SUFFIX}, | |
182 | {"block-size", 1, 0, 'B'}, | |
183 | {"timeout", 1, 0, OPT_TIMEOUT}, | |
184 | {"temp-dir", 1, 0, 'T'}, | |
185 | {"compress", 0, 0, 'z'}, | |
186 | {"daemon", 0, 0, OPT_DAEMON}, | |
a800434a | 187 | {"stats", 0, 0, OPT_STATS}, |
c95da96a | 188 | {"partial", 0, 0, OPT_PARTIAL}, |
7a6421fa AT |
189 | {"config", 1, 0, OPT_CONFIG}, |
190 | {"port", 1, 0, OPT_PORT}, | |
191 | {0,0,0,0}}; | |
192 | ||
193 | void parse_arguments(int argc, char *argv[]) | |
194 | { | |
195 | int opt; | |
196 | int option_index; | |
197 | ||
198 | while ((opt = getopt_long(argc, argv, | |
199 | short_options, long_options, &option_index)) | |
200 | != -1) { | |
201 | switch (opt) | |
202 | { | |
203 | case OPT_VERSION: | |
704f908e | 204 | printf("rsync version %s protocol version %d\n\n", |
7a6421fa | 205 | VERSION,PROTOCOL_VERSION); |
704f908e | 206 | printf("Written by Andrew Tridgell and Paul Mackerras\n"); |
7a6421fa AT |
207 | exit_cleanup(0); |
208 | ||
209 | case OPT_SUFFIX: | |
210 | backup_suffix = optarg; | |
211 | break; | |
212 | ||
213 | case OPT_RSYNC_PATH: | |
214 | rsync_path = optarg; | |
215 | break; | |
216 | ||
217 | case 'I': | |
218 | ignore_times = 1; | |
219 | break; | |
220 | ||
221 | case 'x': | |
222 | one_file_system=1; | |
223 | break; | |
224 | ||
225 | case OPT_DELETE: | |
226 | delete_mode = 1; | |
227 | break; | |
228 | ||
229 | case OPT_FORCE: | |
230 | force_delete = 1; | |
231 | break; | |
232 | ||
233 | case OPT_NUMERIC_IDS: | |
234 | numeric_ids = 1; | |
235 | break; | |
236 | ||
237 | case OPT_EXCLUDE: | |
2b6b4d53 AT |
238 | add_exclude(optarg, 0); |
239 | break; | |
240 | ||
241 | case OPT_INCLUDE: | |
242 | add_exclude(optarg, 1); | |
7a6421fa AT |
243 | break; |
244 | ||
245 | case OPT_EXCLUDE_FROM: | |
2b6b4d53 AT |
246 | add_exclude_file(optarg,1, 0); |
247 | break; | |
248 | ||
249 | case OPT_INCLUDE_FROM: | |
250 | add_exclude_file(optarg,1, 1); | |
7a6421fa AT |
251 | break; |
252 | ||
253 | case 'h': | |
254 | usage(FINFO); | |
255 | exit_cleanup(0); | |
256 | ||
257 | case 'b': | |
258 | make_backups=1; | |
259 | break; | |
260 | ||
261 | case 'n': | |
262 | dry_run=1; | |
263 | break; | |
264 | ||
265 | case 'S': | |
266 | sparse_files=1; | |
267 | break; | |
268 | ||
269 | case 'C': | |
270 | cvs_exclude=1; | |
271 | break; | |
272 | ||
273 | case 'u': | |
274 | update_only=1; | |
275 | break; | |
276 | ||
277 | case 'l': | |
278 | preserve_links=1; | |
279 | break; | |
280 | ||
281 | case 'L': | |
282 | copy_links=1; | |
283 | break; | |
284 | ||
285 | case 'W': | |
286 | whole_file=1; | |
287 | break; | |
288 | ||
289 | case 'H': | |
290 | #if SUPPORT_HARD_LINKS | |
291 | preserve_hard_links=1; | |
292 | #else | |
293 | rprintf(FERROR,"ERROR: hard links not supported on this platform\n"); | |
294 | exit_cleanup(1); | |
295 | #endif | |
296 | break; | |
297 | ||
298 | case 'p': | |
299 | preserve_perms=1; | |
300 | break; | |
301 | ||
302 | case 'o': | |
303 | preserve_uid=1; | |
304 | break; | |
305 | ||
306 | case 'g': | |
307 | preserve_gid=1; | |
308 | break; | |
309 | ||
310 | case 'D': | |
311 | preserve_devices=1; | |
312 | break; | |
313 | ||
314 | case 't': | |
315 | preserve_times=1; | |
316 | break; | |
317 | ||
318 | case 'c': | |
319 | always_checksum=1; | |
320 | break; | |
321 | ||
322 | case 'v': | |
323 | verbose++; | |
324 | break; | |
325 | ||
326 | case 'a': | |
327 | recurse=1; | |
328 | #if SUPPORT_LINKS | |
329 | preserve_links=1; | |
330 | #endif | |
331 | preserve_perms=1; | |
332 | preserve_times=1; | |
333 | preserve_gid=1; | |
334 | if (am_root) { | |
335 | preserve_devices=1; | |
336 | preserve_uid=1; | |
337 | } | |
338 | break; | |
339 | ||
340 | case OPT_SERVER: | |
341 | am_server = 1; | |
342 | break; | |
343 | ||
344 | case OPT_SENDER: | |
345 | if (!am_server) { | |
346 | usage(FERROR); | |
347 | exit_cleanup(1); | |
348 | } | |
349 | am_sender = 1; | |
350 | break; | |
351 | ||
352 | case 'r': | |
353 | recurse = 1; | |
354 | break; | |
355 | ||
356 | case 'R': | |
357 | relative_paths = 1; | |
358 | break; | |
359 | ||
360 | case 'e': | |
361 | shell_cmd = optarg; | |
362 | break; | |
363 | ||
364 | case 'B': | |
365 | block_size = atoi(optarg); | |
366 | break; | |
367 | ||
368 | case OPT_TIMEOUT: | |
369 | io_timeout = atoi(optarg); | |
370 | break; | |
371 | ||
372 | case 'T': | |
373 | tmpdir = optarg; | |
374 | break; | |
375 | ||
376 | case 'z': | |
377 | do_compression = 1; | |
378 | break; | |
379 | ||
380 | case OPT_DAEMON: | |
381 | am_daemon = 1; | |
382 | break; | |
383 | ||
a800434a AT |
384 | case OPT_STATS: |
385 | do_stats = 1; | |
386 | break; | |
387 | ||
c95da96a AT |
388 | case OPT_PARTIAL: |
389 | keep_partial = 1; | |
390 | break; | |
391 | ||
7a6421fa AT |
392 | case OPT_CONFIG: |
393 | config_file = optarg; | |
394 | break; | |
395 | ||
396 | case OPT_PORT: | |
397 | rsync_port = atoi(optarg); | |
398 | break; | |
399 | ||
400 | default: | |
401 | /* rprintf(FERROR,"bad option -%c\n",opt); */ | |
402 | exit_cleanup(1); | |
403 | } | |
404 | } | |
405 | } | |
406 | ||
407 | ||
408 | void server_options(char **args,int *argc) | |
409 | { | |
410 | int ac = *argc; | |
411 | static char argstr[50]; | |
412 | static char bsize[30]; | |
413 | static char iotime[30]; | |
414 | int i, x; | |
415 | ||
416 | args[ac++] = "--server"; | |
417 | ||
418 | if (!am_sender) | |
419 | args[ac++] = "--sender"; | |
420 | ||
421 | x = 1; | |
422 | argstr[0] = '-'; | |
423 | for (i=0;i<verbose;i++) | |
424 | argstr[x++] = 'v'; | |
425 | if (make_backups) | |
426 | argstr[x++] = 'b'; | |
427 | if (update_only) | |
428 | argstr[x++] = 'u'; | |
429 | if (dry_run) | |
430 | argstr[x++] = 'n'; | |
431 | if (preserve_links) | |
432 | argstr[x++] = 'l'; | |
433 | if (copy_links) | |
434 | argstr[x++] = 'L'; | |
435 | if (whole_file) | |
436 | argstr[x++] = 'W'; | |
437 | if (preserve_hard_links) | |
438 | argstr[x++] = 'H'; | |
439 | if (preserve_uid) | |
440 | argstr[x++] = 'o'; | |
441 | if (preserve_gid) | |
442 | argstr[x++] = 'g'; | |
443 | if (preserve_devices) | |
444 | argstr[x++] = 'D'; | |
445 | if (preserve_times) | |
446 | argstr[x++] = 't'; | |
447 | if (preserve_perms) | |
448 | argstr[x++] = 'p'; | |
449 | if (recurse) | |
450 | argstr[x++] = 'r'; | |
451 | if (always_checksum) | |
452 | argstr[x++] = 'c'; | |
453 | if (cvs_exclude) | |
454 | argstr[x++] = 'C'; | |
455 | if (ignore_times) | |
456 | argstr[x++] = 'I'; | |
457 | if (relative_paths) | |
458 | argstr[x++] = 'R'; | |
459 | if (one_file_system) | |
460 | argstr[x++] = 'x'; | |
461 | if (sparse_files) | |
462 | argstr[x++] = 'S'; | |
463 | if (do_compression) | |
464 | argstr[x++] = 'z'; | |
465 | argstr[x] = 0; | |
466 | ||
467 | if (x != 1) args[ac++] = argstr; | |
468 | ||
469 | if (block_size != BLOCK_SIZE) { | |
470 | sprintf(bsize,"-B%d",block_size); | |
471 | args[ac++] = bsize; | |
472 | } | |
473 | ||
474 | if (io_timeout) { | |
475 | sprintf(iotime,"--timeout=%d",io_timeout); | |
476 | args[ac++] = iotime; | |
477 | } | |
478 | ||
479 | if (strcmp(backup_suffix, BACKUP_SUFFIX)) { | |
480 | args[ac++] = "--suffix"; | |
481 | args[ac++] = backup_suffix; | |
482 | } | |
483 | ||
484 | if (delete_mode) | |
485 | args[ac++] = "--delete"; | |
486 | ||
c95da96a AT |
487 | if (keep_partial) |
488 | args[ac++] = "--partial"; | |
489 | ||
7a6421fa AT |
490 | if (force_delete) |
491 | args[ac++] = "--force"; | |
492 | ||
493 | if (numeric_ids) | |
494 | args[ac++] = "--numeric-ids"; | |
495 | ||
496 | if (tmpdir) { | |
497 | args[ac++] = "--temp-dir"; | |
498 | args[ac++] = tmpdir; | |
499 | } | |
500 | ||
501 | *argc = ac; | |
502 | } | |
503 |