Commit | Line | Data |
---|---|---|
c627d613 AT |
1 | /* |
2 | Copyright (C) Andrew Tridgell 1996 | |
3 | Copyright (C) Paul Mackerras 1996 | |
4 | ||
5 | This program is free software; you can redistribute it and/or modify | |
6 | it under the terms of the GNU General Public License as published by | |
7 | the Free Software Foundation; either version 2 of the License, or | |
8 | (at your option) any later version. | |
9 | ||
10 | This program is distributed in the hope that it will be useful, | |
11 | but WITHOUT ANY WARRANTY; without even the implied warranty of | |
12 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
13 | GNU General Public License for more details. | |
14 | ||
15 | You should have received a copy of the GNU General Public License | |
16 | along with this program; if not, write to the Free Software | |
17 | Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. | |
18 | */ | |
19 | ||
20 | #include "rsync.h" | |
21 | ||
22 | int verbose = 0; | |
23 | int always_checksum = 0; | |
24 | time_t starttime; | |
71c46176 | 25 | int64 total_size = 0; |
c627d613 AT |
26 | int block_size=BLOCK_SIZE; |
27 | ||
28 | char *backup_suffix = BACKUP_SUFFIX; | |
950ab32d | 29 | char *tmpdir = NULL; |
c627d613 AT |
30 | |
31 | static char *rsync_path = RSYNC_NAME; | |
32 | ||
33 | int make_backups = 0; | |
82306bf6 AT |
34 | int whole_file = 0; |
35 | int copy_links = 0; | |
c627d613 | 36 | int preserve_links = 0; |
dc5ddbcc | 37 | int preserve_hard_links = 0; |
c627d613 AT |
38 | int preserve_perms = 0; |
39 | int preserve_devices = 0; | |
40 | int preserve_uid = 0; | |
41 | int preserve_gid = 0; | |
42 | int preserve_times = 0; | |
43 | int update_only = 0; | |
44 | int cvs_exclude = 0; | |
45 | int dry_run=0; | |
46 | int local_server=0; | |
47 | int ignore_times=0; | |
48 | int delete_mode=0; | |
49 | int one_file_system=0; | |
4fe159a8 | 50 | int remote_version=0; |
dc5ddbcc | 51 | int sparse_files=0; |
70d794dc | 52 | int do_compression=0; |
7b8356d0 AT |
53 | int am_root=0; |
54 | int orig_umask=0; | |
6574b4f7 | 55 | int relative_paths=0; |
f6c34742 | 56 | int numeric_ids = 0; |
3cb6f5d6 | 57 | int force_delete = 0; |
6ba9279f AT |
58 | int io_timeout = 0; |
59 | int io_error = 0; | |
70d794dc | 60 | |
34ccb63e | 61 | extern int csum_length; |
c627d613 AT |
62 | |
63 | int am_server = 0; | |
366345fe | 64 | int am_sender; |
c627d613 AT |
65 | int recurse = 0; |
66 | ||
67 | static void usage(FILE *f); | |
68 | ||
69 | static void report(int f) | |
70 | { | |
71c46176 | 71 | int64 in,out,tsize; |
c627d613 AT |
72 | time_t t = time(NULL); |
73 | ||
74 | if (!verbose) return; | |
75 | ||
366345fe | 76 | if (am_server && am_sender) { |
3a6a366f AT |
77 | write_longint(f,read_total()); |
78 | write_longint(f,write_total()); | |
79 | write_longint(f,total_size); | |
c627d613 AT |
80 | write_flush(f); |
81 | return; | |
82 | } | |
83 | ||
366345fe | 84 | if (am_sender) { |
c627d613 AT |
85 | in = read_total(); |
86 | out = write_total(); | |
3a6a366f | 87 | tsize = total_size; |
c627d613 | 88 | } else { |
3a6a366f AT |
89 | in = read_longint(f); |
90 | out = read_longint(f); | |
91 | tsize = read_longint(f); | |
c627d613 AT |
92 | } |
93 | ||
0d0e2e93 | 94 | #if HAVE_LONGLONG |
31730241 | 95 | printf("wrote %lld bytes read %lld bytes %.2f bytes/sec\n", |
0d0e2e93 | 96 | (long long)out,(long long)in,(in+out)/(0.5 + (t-starttime))); |
31730241 | 97 | printf("total size is %lld speedup is %.2f\n", |
0d0e2e93 AT |
98 | (long long)tsize,(1.0*tsize)/(in+out)); |
99 | #else | |
31730241 | 100 | printf("wrote %ld bytes read %ld bytes %.2f bytes/sec\n", |
3a6a366f | 101 | (long)out,(long)in,(in+out)/(0.5 + (t-starttime))); |
31730241 | 102 | printf("total size is %ld speedup is %.2f\n", |
3a6a366f | 103 | (long)tsize,(1.0*tsize)/(in+out)); |
0d0e2e93 | 104 | #endif |
c627d613 AT |
105 | } |
106 | ||
107 | ||
108 | static void server_options(char **args,int *argc) | |
109 | { | |
110 | int ac = *argc; | |
111 | static char argstr[50]; | |
112 | static char bsize[30]; | |
6ba9279f | 113 | static char iotime[30]; |
c627d613 AT |
114 | int i, x; |
115 | ||
116 | args[ac++] = "--server"; | |
117 | ||
366345fe | 118 | if (!am_sender) |
c627d613 AT |
119 | args[ac++] = "--sender"; |
120 | ||
121 | x = 1; | |
122 | argstr[0] = '-'; | |
123 | for (i=0;i<verbose;i++) | |
124 | argstr[x++] = 'v'; | |
125 | if (make_backups) | |
126 | argstr[x++] = 'b'; | |
127 | if (update_only) | |
128 | argstr[x++] = 'u'; | |
129 | if (dry_run) | |
130 | argstr[x++] = 'n'; | |
131 | if (preserve_links) | |
132 | argstr[x++] = 'l'; | |
82306bf6 AT |
133 | if (copy_links) |
134 | argstr[x++] = 'L'; | |
135 | if (whole_file) | |
136 | argstr[x++] = 'W'; | |
dc5ddbcc AT |
137 | if (preserve_hard_links) |
138 | argstr[x++] = 'H'; | |
c627d613 AT |
139 | if (preserve_uid) |
140 | argstr[x++] = 'o'; | |
141 | if (preserve_gid) | |
142 | argstr[x++] = 'g'; | |
143 | if (preserve_devices) | |
144 | argstr[x++] = 'D'; | |
145 | if (preserve_times) | |
146 | argstr[x++] = 't'; | |
147 | if (preserve_perms) | |
148 | argstr[x++] = 'p'; | |
149 | if (recurse) | |
150 | argstr[x++] = 'r'; | |
151 | if (always_checksum) | |
152 | argstr[x++] = 'c'; | |
153 | if (cvs_exclude) | |
154 | argstr[x++] = 'C'; | |
155 | if (ignore_times) | |
156 | argstr[x++] = 'I'; | |
6574b4f7 AT |
157 | if (relative_paths) |
158 | argstr[x++] = 'R'; | |
c627d613 AT |
159 | if (one_file_system) |
160 | argstr[x++] = 'x'; | |
dc5ddbcc AT |
161 | if (sparse_files) |
162 | argstr[x++] = 'S'; | |
861c20b4 PM |
163 | if (do_compression) |
164 | argstr[x++] = 'z'; | |
c627d613 AT |
165 | argstr[x] = 0; |
166 | ||
167 | if (x != 1) args[ac++] = argstr; | |
168 | ||
169 | if (block_size != BLOCK_SIZE) { | |
170 | sprintf(bsize,"-B%d",block_size); | |
171 | args[ac++] = bsize; | |
172 | } | |
43a481dc | 173 | |
6ba9279f AT |
174 | if (io_timeout) { |
175 | sprintf(iotime,"--timeout=%d",io_timeout); | |
176 | args[ac++] = iotime; | |
177 | } | |
178 | ||
dc7a9478 AT |
179 | if (strcmp(backup_suffix, BACKUP_SUFFIX)) { |
180 | args[ac++] = "--suffix"; | |
181 | args[ac++] = backup_suffix; | |
182 | } | |
183 | ||
c627d613 AT |
184 | if (delete_mode) |
185 | args[ac++] = "--delete"; | |
186 | ||
3cb6f5d6 AT |
187 | if (force_delete) |
188 | args[ac++] = "--force"; | |
189 | ||
f6c34742 AT |
190 | if (numeric_ids) |
191 | args[ac++] = "--numeric-ids"; | |
192 | ||
950ab32d AT |
193 | if (tmpdir) { |
194 | args[ac++] = "--temp-dir"; | |
195 | args[ac++] = tmpdir; | |
196 | } | |
197 | ||
c627d613 AT |
198 | *argc = ac; |
199 | } | |
200 | ||
201 | ||
202 | ||
e3cd198f | 203 | static int do_cmd(char *cmd,char *machine,char *user,char *path,int *f_in,int *f_out) |
c627d613 | 204 | { |
366345fe AT |
205 | char *args[100]; |
206 | int i,argc=0, ret; | |
207 | char *tok,*dir=NULL; | |
208 | ||
209 | if (!local_server) { | |
210 | if (!cmd) | |
211 | cmd = getenv(RSYNC_RSH_ENV); | |
212 | if (!cmd) | |
213 | cmd = RSYNC_RSH; | |
214 | cmd = strdup(cmd); | |
215 | if (!cmd) | |
216 | goto oom; | |
217 | ||
218 | for (tok=strtok(cmd," ");tok;tok=strtok(NULL," ")) { | |
219 | args[argc++] = tok; | |
220 | } | |
c627d613 | 221 | |
7b8356d0 | 222 | #if HAVE_REMSH |
366345fe AT |
223 | /* remsh (on HPUX) takes the arguments the other way around */ |
224 | args[argc++] = machine; | |
225 | if (user) { | |
226 | args[argc++] = "-l"; | |
227 | args[argc++] = user; | |
228 | } | |
7b8356d0 | 229 | #else |
366345fe AT |
230 | if (user) { |
231 | args[argc++] = "-l"; | |
232 | args[argc++] = user; | |
233 | } | |
234 | args[argc++] = machine; | |
7b8356d0 | 235 | #endif |
c627d613 | 236 | |
366345fe | 237 | args[argc++] = rsync_path; |
c627d613 | 238 | |
366345fe AT |
239 | server_options(args,&argc); |
240 | } | |
c627d613 | 241 | |
366345fe | 242 | args[argc++] = "."; |
76076c4b | 243 | |
366345fe AT |
244 | if (path && *path) |
245 | args[argc++] = path; | |
c627d613 | 246 | |
366345fe | 247 | args[argc] = NULL; |
c627d613 | 248 | |
366345fe AT |
249 | if (verbose > 3) { |
250 | fprintf(FINFO,"cmd="); | |
251 | for (i=0;i<argc;i++) | |
252 | fprintf(FINFO,"%s ",args[i]); | |
253 | fprintf(FINFO,"\n"); | |
254 | } | |
255 | ||
256 | if (local_server) { | |
257 | ret = local_child(argc, args, f_in, f_out); | |
258 | } else { | |
259 | ret = piped_child(args,f_in,f_out); | |
260 | } | |
c627d613 | 261 | |
366345fe | 262 | if (dir) free(dir); |
82306bf6 | 263 | |
366345fe | 264 | return ret; |
c627d613 AT |
265 | |
266 | oom: | |
366345fe AT |
267 | out_of_memory("do_cmd"); |
268 | return 0; /* not reached */ | |
c627d613 AT |
269 | } |
270 | ||
271 | ||
272 | ||
273 | ||
274 | static char *get_local_name(struct file_list *flist,char *name) | |
275 | { | |
bcacc18b | 276 | STRUCT_STAT st; |
c627d613 | 277 | |
bcacc18b | 278 | if (do_stat(name,&st) == 0) { |
c627d613 AT |
279 | if (S_ISDIR(st.st_mode)) { |
280 | if (chdir(name) != 0) { | |
6574b4f7 | 281 | fprintf(FERROR,"chdir %s : %s (1)\n",name,strerror(errno)); |
34ccb63e | 282 | exit_cleanup(1); |
c627d613 AT |
283 | } |
284 | return NULL; | |
285 | } | |
286 | if (flist->count > 1) { | |
dc5ddbcc | 287 | fprintf(FERROR,"ERROR: destination must be a directory when copying more than 1 file\n"); |
34ccb63e | 288 | exit_cleanup(1); |
c627d613 AT |
289 | } |
290 | return name; | |
291 | } | |
292 | ||
293 | if (flist->count == 1) | |
294 | return name; | |
295 | ||
296 | if (!name) | |
297 | return NULL; | |
298 | ||
1b2d733a | 299 | if (do_mkdir(name,0777 & ~orig_umask) != 0) { |
6574b4f7 | 300 | fprintf(FERROR,"mkdir %s : %s (1)\n",name,strerror(errno)); |
34ccb63e | 301 | exit_cleanup(1); |
94481d91 | 302 | } else { |
dc5ddbcc | 303 | fprintf(FINFO,"created directory %s\n",name); |
c627d613 AT |
304 | } |
305 | ||
306 | if (chdir(name) != 0) { | |
6574b4f7 | 307 | fprintf(FERROR,"chdir %s : %s (2)\n",name,strerror(errno)); |
34ccb63e | 308 | exit_cleanup(1); |
c627d613 AT |
309 | } |
310 | ||
311 | return NULL; | |
312 | } | |
313 | ||
314 | ||
315 | ||
316 | ||
317 | void do_server_sender(int argc,char *argv[]) | |
318 | { | |
319 | int i; | |
c627d613 | 320 | struct file_list *flist; |
76076c4b | 321 | char *dir = argv[0]; |
c627d613 AT |
322 | |
323 | if (verbose > 2) | |
6ba9279f | 324 | fprintf(FINFO,"server_sender starting pid=%d\n",(int)getpid()); |
c627d613 | 325 | |
6574b4f7 | 326 | if (!relative_paths && chdir(dir) != 0) { |
76076c4b AT |
327 | fprintf(FERROR,"chdir %s: %s (3)\n",dir,strerror(errno)); |
328 | exit_cleanup(1); | |
c627d613 AT |
329 | } |
330 | argc--; | |
331 | argv++; | |
332 | ||
333 | if (strcmp(dir,".")) { | |
76076c4b AT |
334 | int l = strlen(dir); |
335 | if (strcmp(dir,"/") == 0) | |
336 | l = 0; | |
337 | for (i=0;i<argc;i++) | |
338 | argv[i] += l+1; | |
c627d613 AT |
339 | } |
340 | ||
341 | if (argc == 0 && recurse) { | |
76076c4b AT |
342 | argc=1; |
343 | argv--; | |
344 | argv[0] = "."; | |
c627d613 AT |
345 | } |
346 | ||
347 | ||
a06d19e3 | 348 | flist = send_file_list(STDOUT_FILENO,argc,argv); |
c627d613 AT |
349 | send_files(flist,STDOUT_FILENO,STDIN_FILENO); |
350 | report(STDOUT_FILENO); | |
34ccb63e | 351 | exit_cleanup(0); |
c627d613 AT |
352 | } |
353 | ||
354 | ||
dc5ddbcc AT |
355 | static int do_recv(int f_in,int f_out,struct file_list *flist,char *local_name) |
356 | { | |
357 | int pid; | |
358 | int status=0; | |
c6e7fcb4 | 359 | int recv_pipe[2]; |
dc5ddbcc AT |
360 | |
361 | if (preserve_hard_links) | |
362 | init_hard_links(flist); | |
363 | ||
c6e7fcb4 AT |
364 | if (pipe(recv_pipe) < 0) { |
365 | fprintf(FERROR,"pipe failed in do_recv\n"); | |
366 | exit(1); | |
367 | } | |
368 | ||
369 | ||
3ba62a83 | 370 | if ((pid=do_fork()) == 0) { |
c6e7fcb4 | 371 | recv_files(f_in,flist,local_name,recv_pipe[1]); |
dc5ddbcc | 372 | if (verbose > 2) |
6ba9279f | 373 | fprintf(FINFO,"receiver read %ld\n",(long)read_total()); |
dc5ddbcc AT |
374 | exit_cleanup(0); |
375 | } | |
376 | ||
c6e7fcb4 | 377 | generate_files(f_out,flist,local_name,recv_pipe[0]); |
dc5ddbcc AT |
378 | |
379 | waitpid(pid, &status, 0); | |
380 | ||
381 | return status; | |
382 | } | |
383 | ||
c627d613 AT |
384 | |
385 | void do_server_recv(int argc,char *argv[]) | |
386 | { | |
dc5ddbcc | 387 | int status; |
c627d613 AT |
388 | struct file_list *flist; |
389 | char *local_name=NULL; | |
76076c4b | 390 | char *dir = NULL; |
c627d613 AT |
391 | |
392 | if (verbose > 2) | |
6ba9279f | 393 | fprintf(FINFO,"server_recv(%d) starting pid=%d\n",argc,(int)getpid()); |
c627d613 AT |
394 | |
395 | if (argc > 0) { | |
76076c4b AT |
396 | dir = argv[0]; |
397 | argc--; | |
398 | argv++; | |
399 | if (chdir(dir) != 0) { | |
400 | fprintf(FERROR,"chdir %s : %s (4)\n", | |
401 | dir,strerror(errno)); | |
402 | exit_cleanup(1); | |
403 | } | |
c627d613 AT |
404 | } |
405 | ||
406 | if (delete_mode) | |
407 | recv_exclude_list(STDIN_FILENO); | |
408 | ||
409 | flist = recv_file_list(STDIN_FILENO); | |
410 | if (!flist || flist->count == 0) { | |
dc5ddbcc | 411 | fprintf(FERROR,"nothing to do\n"); |
34ccb63e | 412 | exit_cleanup(1); |
c627d613 AT |
413 | } |
414 | ||
415 | if (argc > 0) { | |
76076c4b AT |
416 | if (strcmp(dir,".")) { |
417 | argv[0] += strlen(dir); | |
418 | if (argv[0][0] == '/') argv[0]++; | |
419 | } | |
420 | local_name = get_local_name(flist,argv[0]); | |
c627d613 AT |
421 | } |
422 | ||
dc5ddbcc | 423 | status = do_recv(STDIN_FILENO,STDOUT_FILENO,flist,local_name); |
34ccb63e | 424 | exit_cleanup(status); |
c627d613 AT |
425 | } |
426 | ||
427 | ||
366345fe AT |
428 | void start_server(int argc, char *argv[]) |
429 | { | |
430 | setup_protocol(STDOUT_FILENO,STDIN_FILENO); | |
431 | ||
432 | if (am_sender) { | |
433 | recv_exclude_list(STDIN_FILENO); | |
434 | if (cvs_exclude) | |
435 | add_cvs_excludes(); | |
436 | do_server_sender(argc,argv); | |
437 | } else { | |
438 | do_server_recv(argc,argv); | |
439 | } | |
440 | exit_cleanup(0); | |
441 | } | |
442 | ||
443 | ||
c627d613 AT |
444 | static void usage(FILE *f) |
445 | { | |
446 | fprintf(f,"rsync version %s Copyright Andrew Tridgell and Paul Mackerras\n\n", | |
447 | VERSION); | |
448 | fprintf(f,"Usage:\t%s [options] src user@host:dest\nOR",RSYNC_NAME); | |
449 | fprintf(f,"\t%s [options] user@host:src dest\n\n",RSYNC_NAME); | |
450 | fprintf(f,"Options:\n"); | |
451 | fprintf(f,"-v, --verbose increase verbosity\n"); | |
452 | fprintf(f,"-c, --checksum always checksum\n"); | |
453 | fprintf(f,"-a, --archive archive mode (same as -rlptDog)\n"); | |
454 | fprintf(f,"-r, --recursive recurse into directories\n"); | |
6574b4f7 | 455 | fprintf(f,"-R, --relative use relative path names\n"); |
c627d613 AT |
456 | fprintf(f,"-b, --backup make backups (default ~ extension)\n"); |
457 | fprintf(f,"-u, --update update only (don't overwrite newer files)\n"); | |
458 | fprintf(f,"-l, --links preserve soft links\n"); | |
82306bf6 | 459 | fprintf(f,"-L, --copy-links treat soft links like regular files\n"); |
dc5ddbcc | 460 | fprintf(f,"-H, --hard-links preserve hard links\n"); |
c627d613 AT |
461 | fprintf(f,"-p, --perms preserve permissions\n"); |
462 | fprintf(f,"-o, --owner preserve owner (root only)\n"); | |
463 | fprintf(f,"-g, --group preserve group\n"); | |
464 | fprintf(f,"-D, --devices preserve devices (root only)\n"); | |
465 | fprintf(f,"-t, --times preserve times\n"); | |
dc5ddbcc | 466 | fprintf(f,"-S, --sparse handle sparse files efficiently\n"); |
c627d613 | 467 | fprintf(f,"-n, --dry-run show what would have been transferred\n"); |
82306bf6 | 468 | fprintf(f,"-W, --whole-file copy whole files, no incremental checks\n"); |
c627d613 AT |
469 | fprintf(f,"-x, --one-file-system don't cross filesystem boundaries\n"); |
470 | fprintf(f,"-B, --block-size SIZE checksum blocking size\n"); | |
471 | fprintf(f,"-e, --rsh COMMAND specify rsh replacement\n"); | |
472 | fprintf(f," --rsync-path PATH specify path to rsync on the remote machine\n"); | |
473 | fprintf(f,"-C, --cvs-exclude auto ignore files in the same way CVS does\n"); | |
474 | fprintf(f," --delete delete files that don't exist on the sending side\n"); | |
3cb6f5d6 | 475 | fprintf(f," --force force deletion of directories even if not empty\n"); |
f6c34742 | 476 | fprintf(f," --numeric-ids don't map uid/gid values by user/group name\n"); |
6ba9279f | 477 | fprintf(f," --timeout TIME set IO timeout in seconds\n"); |
c627d613 | 478 | fprintf(f,"-I, --ignore-times don't exclude files that match length and time\n"); |
950ab32d | 479 | fprintf(f,"-T --temp-dir DIR create temporary files in directory DIR\n"); |
861c20b4 | 480 | fprintf(f,"-z, --compress compress file data\n"); |
c627d613 AT |
481 | fprintf(f," --exclude FILE exclude file FILE\n"); |
482 | fprintf(f," --exclude-from FILE exclude files listed in FILE\n"); | |
483 | fprintf(f," --suffix SUFFIX override backup suffix\n"); | |
484 | fprintf(f," --version print version number\n"); | |
485 | ||
486 | fprintf(f,"\n"); | |
487 | fprintf(f,"the backup suffix defaults to %s\n",BACKUP_SUFFIX); | |
488 | fprintf(f,"the block size defaults to %d\n",BLOCK_SIZE); | |
489 | } | |
490 | ||
491 | enum {OPT_VERSION,OPT_SUFFIX,OPT_SENDER,OPT_SERVER,OPT_EXCLUDE, | |
6ba9279f AT |
492 | OPT_EXCLUDE_FROM,OPT_DELETE,OPT_NUMERIC_IDS,OPT_RSYNC_PATH, |
493 | OPT_FORCE,OPT_TIMEOUT}; | |
c627d613 | 494 | |
950ab32d | 495 | static char *short_options = "oblLWHpguDCtcahvrRIxnSe:B:T:z"; |
c627d613 AT |
496 | |
497 | static struct option long_options[] = { | |
498 | {"version", 0, 0, OPT_VERSION}, | |
499 | {"server", 0, 0, OPT_SERVER}, | |
500 | {"sender", 0, 0, OPT_SENDER}, | |
501 | {"delete", 0, 0, OPT_DELETE}, | |
3cb6f5d6 | 502 | {"force", 0, 0, OPT_FORCE}, |
f6c34742 | 503 | {"numeric-ids", 0, 0, OPT_NUMERIC_IDS}, |
c627d613 AT |
504 | {"exclude", 1, 0, OPT_EXCLUDE}, |
505 | {"exclude-from",1, 0, OPT_EXCLUDE_FROM}, | |
506 | {"rsync-path", 1, 0, OPT_RSYNC_PATH}, | |
507 | {"one-file-system",0, 0, 'x'}, | |
508 | {"ignore-times",0, 0, 'I'}, | |
509 | {"help", 0, 0, 'h'}, | |
510 | {"dry-run", 0, 0, 'n'}, | |
dc5ddbcc | 511 | {"sparse", 0, 0, 'S'}, |
c627d613 AT |
512 | {"cvs-exclude", 0, 0, 'C'}, |
513 | {"archive", 0, 0, 'a'}, | |
514 | {"checksum", 0, 0, 'c'}, | |
515 | {"backup", 0, 0, 'b'}, | |
516 | {"update", 0, 0, 'u'}, | |
517 | {"verbose", 0, 0, 'v'}, | |
518 | {"recursive", 0, 0, 'r'}, | |
6574b4f7 | 519 | {"relative", 0, 0, 'R'}, |
c627d613 AT |
520 | {"devices", 0, 0, 'D'}, |
521 | {"perms", 0, 0, 'p'}, | |
522 | {"links", 0, 0, 'l'}, | |
82306bf6 AT |
523 | {"copy-links", 0, 0, 'L'}, |
524 | {"whole-file", 0, 0, 'W'}, | |
dc5ddbcc | 525 | {"hard-links", 0, 0, 'H'}, |
c627d613 AT |
526 | {"owner", 0, 0, 'o'}, |
527 | {"group", 0, 0, 'g'}, | |
528 | {"times", 0, 0, 't'}, | |
529 | {"rsh", 1, 0, 'e'}, | |
530 | {"suffix", 1, 0, OPT_SUFFIX}, | |
531 | {"block-size", 1, 0, 'B'}, | |
6ba9279f | 532 | {"timeout", 1, 0, OPT_TIMEOUT}, |
950ab32d | 533 | {"temp-dir", 1, 0, 'T'}, |
861c20b4 | 534 | {"compress", 0, 0, 'z'}, |
c627d613 AT |
535 | {0,0,0,0}}; |
536 | ||
82306bf6 AT |
537 | RETSIGTYPE sigusr1_handler(int val) { |
538 | exit_cleanup(1); | |
539 | } | |
540 | ||
c627d613 AT |
541 | int main(int argc,char *argv[]) |
542 | { | |
774ef68f | 543 | int pid, status = 0, status2 = 0; |
c627d613 AT |
544 | int opt; |
545 | int option_index; | |
546 | char *shell_cmd = NULL; | |
547 | char *shell_machine = NULL; | |
548 | char *shell_path = NULL; | |
549 | char *shell_user = NULL; | |
550 | char *p; | |
551 | int f_in,f_out; | |
552 | struct file_list *flist; | |
553 | char *local_name = NULL; | |
554 | ||
82306bf6 AT |
555 | signal(SIGUSR1, sigusr1_handler); |
556 | ||
c627d613 | 557 | starttime = time(NULL); |
7b8356d0 AT |
558 | am_root = (getuid() == 0); |
559 | ||
560 | /* we set a 0 umask so that correct file permissions can be | |
561 | carried across */ | |
3a6a366f | 562 | orig_umask = (int)umask(0); |
c627d613 AT |
563 | |
564 | while ((opt = getopt_long(argc, argv, | |
565 | short_options, long_options, &option_index)) | |
566 | != -1) { | |
567 | switch (opt) | |
568 | { | |
569 | case OPT_VERSION: | |
570 | printf("rsync version %s protocol version %d\n", | |
571 | VERSION,PROTOCOL_VERSION); | |
34ccb63e | 572 | exit_cleanup(0); |
c627d613 AT |
573 | |
574 | case OPT_SUFFIX: | |
575 | backup_suffix = optarg; | |
576 | break; | |
577 | ||
578 | case OPT_RSYNC_PATH: | |
579 | rsync_path = optarg; | |
580 | break; | |
581 | ||
582 | case 'I': | |
583 | ignore_times = 1; | |
584 | break; | |
585 | ||
586 | case 'x': | |
587 | one_file_system=1; | |
588 | break; | |
589 | ||
590 | case OPT_DELETE: | |
591 | delete_mode = 1; | |
592 | break; | |
593 | ||
3cb6f5d6 AT |
594 | case OPT_FORCE: |
595 | force_delete = 1; | |
596 | break; | |
597 | ||
f6c34742 AT |
598 | case OPT_NUMERIC_IDS: |
599 | numeric_ids = 1; | |
600 | break; | |
601 | ||
c627d613 AT |
602 | case OPT_EXCLUDE: |
603 | add_exclude(optarg); | |
604 | break; | |
605 | ||
606 | case OPT_EXCLUDE_FROM: | |
607 | add_exclude_file(optarg,1); | |
608 | break; | |
609 | ||
610 | case 'h': | |
dc5ddbcc | 611 | usage(FINFO); |
34ccb63e | 612 | exit_cleanup(0); |
c627d613 AT |
613 | |
614 | case 'b': | |
615 | make_backups=1; | |
616 | break; | |
617 | ||
618 | case 'n': | |
619 | dry_run=1; | |
620 | break; | |
621 | ||
dc5ddbcc AT |
622 | case 'S': |
623 | sparse_files=1; | |
624 | break; | |
625 | ||
c627d613 AT |
626 | case 'C': |
627 | cvs_exclude=1; | |
628 | break; | |
629 | ||
630 | case 'u': | |
631 | update_only=1; | |
632 | break; | |
633 | ||
c627d613 AT |
634 | case 'l': |
635 | preserve_links=1; | |
636 | break; | |
dc5ddbcc | 637 | |
82306bf6 AT |
638 | case 'L': |
639 | copy_links=1; | |
640 | break; | |
641 | ||
642 | case 'W': | |
643 | whole_file=1; | |
644 | break; | |
645 | ||
dc5ddbcc AT |
646 | case 'H': |
647 | #if SUPPORT_HARD_LINKS | |
648 | preserve_hard_links=1; | |
cbbe4892 AT |
649 | #else |
650 | fprintf(FERROR,"ERROR: hard links not supported on this platform\n"); | |
651 | exit_cleanup(1); | |
c627d613 | 652 | #endif |
dc5ddbcc | 653 | break; |
c627d613 AT |
654 | |
655 | case 'p': | |
656 | preserve_perms=1; | |
657 | break; | |
658 | ||
659 | case 'o': | |
7b8356d0 | 660 | preserve_uid=1; |
c627d613 AT |
661 | break; |
662 | ||
663 | case 'g': | |
664 | preserve_gid=1; | |
665 | break; | |
666 | ||
667 | case 'D': | |
7b8356d0 | 668 | preserve_devices=1; |
c627d613 AT |
669 | break; |
670 | ||
671 | case 't': | |
672 | preserve_times=1; | |
673 | break; | |
674 | ||
675 | case 'c': | |
676 | always_checksum=1; | |
677 | break; | |
678 | ||
679 | case 'v': | |
680 | verbose++; | |
681 | break; | |
682 | ||
683 | case 'a': | |
684 | recurse=1; | |
685 | #if SUPPORT_LINKS | |
686 | preserve_links=1; | |
687 | #endif | |
688 | preserve_perms=1; | |
689 | preserve_times=1; | |
690 | preserve_gid=1; | |
7b8356d0 | 691 | if (am_root) { |
c627d613 AT |
692 | preserve_devices=1; |
693 | preserve_uid=1; | |
7b8356d0 | 694 | } |
c627d613 AT |
695 | break; |
696 | ||
697 | case OPT_SERVER: | |
698 | am_server = 1; | |
699 | break; | |
700 | ||
701 | case OPT_SENDER: | |
702 | if (!am_server) { | |
dc5ddbcc | 703 | usage(FERROR); |
34ccb63e | 704 | exit_cleanup(1); |
c627d613 | 705 | } |
366345fe | 706 | am_sender = 1; |
c627d613 AT |
707 | break; |
708 | ||
709 | case 'r': | |
710 | recurse = 1; | |
711 | break; | |
712 | ||
6574b4f7 AT |
713 | case 'R': |
714 | relative_paths = 1; | |
715 | break; | |
716 | ||
c627d613 AT |
717 | case 'e': |
718 | shell_cmd = optarg; | |
719 | break; | |
720 | ||
721 | case 'B': | |
722 | block_size = atoi(optarg); | |
723 | break; | |
724 | ||
6ba9279f AT |
725 | case OPT_TIMEOUT: |
726 | io_timeout = atoi(optarg); | |
727 | break; | |
728 | ||
950ab32d AT |
729 | case 'T': |
730 | tmpdir = optarg; | |
731 | break; | |
732 | ||
861c20b4 PM |
733 | case 'z': |
734 | do_compression = 1; | |
735 | break; | |
736 | ||
c627d613 | 737 | default: |
861c20b4 | 738 | /* fprintf(FERROR,"bad option -%c\n",opt); */ |
34ccb63e | 739 | exit_cleanup(1); |
c627d613 AT |
740 | } |
741 | } | |
742 | ||
743 | while (optind--) { | |
744 | argc--; | |
745 | argv++; | |
746 | } | |
747 | ||
6b83141d AT |
748 | signal(SIGCHLD,SIG_IGN); |
749 | signal(SIGINT,SIGNAL_CAST sig_int); | |
750 | signal(SIGPIPE,SIGNAL_CAST sig_int); | |
751 | signal(SIGHUP,SIGNAL_CAST sig_int); | |
752 | ||
c627d613 AT |
753 | if (dry_run) |
754 | verbose = MAX(verbose,1); | |
755 | ||
cbbe4892 AT |
756 | #ifndef SUPPORT_LINKS |
757 | if (!am_server && preserve_links) { | |
758 | fprintf(FERROR,"ERROR: symbolic links not supported\n"); | |
759 | exit_cleanup(1); | |
760 | } | |
761 | #endif | |
762 | ||
c627d613 | 763 | if (am_server) { |
366345fe | 764 | start_server(argc, argv); |
c627d613 AT |
765 | } |
766 | ||
767 | if (argc < 2) { | |
dc5ddbcc | 768 | usage(FERROR); |
34ccb63e | 769 | exit_cleanup(1); |
c627d613 AT |
770 | } |
771 | ||
772 | p = strchr(argv[0],':'); | |
773 | ||
774 | if (p) { | |
366345fe | 775 | am_sender = 0; |
c627d613 AT |
776 | *p = 0; |
777 | shell_machine = argv[0]; | |
778 | shell_path = p+1; | |
779 | argc--; | |
780 | argv++; | |
781 | } else { | |
366345fe | 782 | am_sender = 1; |
c627d613 AT |
783 | |
784 | p = strchr(argv[argc-1],':'); | |
785 | if (!p) { | |
786 | local_server = 1; | |
787 | } | |
788 | ||
789 | if (local_server) { | |
790 | shell_machine = NULL; | |
791 | shell_path = argv[argc-1]; | |
792 | } else { | |
793 | *p = 0; | |
794 | shell_machine = argv[argc-1]; | |
795 | shell_path = p+1; | |
796 | } | |
797 | argc--; | |
798 | } | |
799 | ||
800 | if (shell_machine) { | |
801 | p = strchr(shell_machine,'@'); | |
802 | if (p) { | |
803 | *p = 0; | |
804 | shell_user = shell_machine; | |
805 | shell_machine = p+1; | |
806 | } | |
807 | } | |
808 | ||
809 | if (verbose > 3) { | |
6ba9279f | 810 | fprintf(FINFO,"cmd=%s machine=%s user=%s path=%s\n", |
c627d613 AT |
811 | shell_cmd?shell_cmd:"", |
812 | shell_machine?shell_machine:"", | |
813 | shell_user?shell_user:"", | |
814 | shell_path?shell_path:""); | |
815 | } | |
816 | ||
366345fe | 817 | if (!am_sender && argc != 1) { |
dc5ddbcc | 818 | usage(FERROR); |
34ccb63e | 819 | exit_cleanup(1); |
c627d613 AT |
820 | } |
821 | ||
822 | pid = do_cmd(shell_cmd,shell_machine,shell_user,shell_path,&f_in,&f_out); | |
823 | ||
aae43eb3 | 824 | setup_protocol(f_out,f_in); |
4fe159a8 | 825 | |
6dd1782c | 826 | #if HAVE_SETLINEBUF |
aa7ed201 AT |
827 | setlinebuf(FINFO); |
828 | setlinebuf(FERROR); | |
6dd1782c | 829 | #endif |
aa7ed201 | 830 | |
c627d613 | 831 | if (verbose > 3) |
6ba9279f | 832 | fprintf(FINFO,"parent=%d child=%d sender=%d recurse=%d\n", |
366345fe | 833 | (int)getpid(),pid,am_sender,recurse); |
c627d613 | 834 | |
366345fe | 835 | if (am_sender) { |
c627d613 AT |
836 | if (cvs_exclude) |
837 | add_cvs_excludes(); | |
838 | if (delete_mode) | |
839 | send_exclude_list(f_out); | |
a06d19e3 | 840 | flist = send_file_list(f_out,argc,argv); |
c627d613 | 841 | if (verbose > 3) |
6ba9279f | 842 | fprintf(FINFO,"file list sent\n"); |
c627d613 AT |
843 | send_files(flist,f_out,f_in); |
844 | if (verbose > 3) | |
6ba9279f | 845 | fprintf(FINFO,"waiting on %d\n",pid); |
c627d613 AT |
846 | waitpid(pid, &status, 0); |
847 | report(-1); | |
34ccb63e | 848 | exit_cleanup(status); |
c627d613 AT |
849 | } |
850 | ||
851 | send_exclude_list(f_out); | |
852 | ||
853 | flist = recv_file_list(f_in); | |
854 | if (!flist || flist->count == 0) { | |
6ba9279f | 855 | fprintf(FINFO,"nothing to do\n"); |
34ccb63e | 856 | exit_cleanup(0); |
c627d613 AT |
857 | } |
858 | ||
859 | local_name = get_local_name(flist,argv[0]); | |
860 | ||
dc5ddbcc | 861 | status2 = do_recv(f_in,f_out,flist,local_name); |
c627d613 AT |
862 | |
863 | report(f_in); | |
864 | ||
865 | waitpid(pid, &status, 0); | |
866 | ||
867 | return status | status2; | |
868 | } | |
82306bf6 | 869 |