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