moved usage mesage after am_daemon test (I'd broken daemon mode)
[rsync/rsync.git] / main.c
CommitLineData
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
f0fca04e 22time_t starttime = 0;
71c46176 23int64 total_size = 0;
5d6bcd44 24
34ccb63e 25extern int csum_length;
c627d613 26
7a6421fa 27extern int verbose;
c627d613
AT
28
29static void report(int f)
30{
7a6421fa
AT
31 int64 in,out,tsize;
32 time_t t = time(NULL);
33 extern int am_server;
34 extern int am_sender;
248fbb8c 35 extern int am_daemon;
7a6421fa 36
248fbb8c
AT
37 if (am_daemon) {
38 rprintf(FINFO, "wrote %.0f bytes read %.0f bytes total size %.0f\n",
39 (double)write_total(),(double)read_total(),
40 (double)total_size);
7b372642 41 if (f == -1 || !am_sender) return;
248fbb8c
AT
42 }
43
7b372642
AT
44 if (!verbose) return;
45
7a6421fa
AT
46 if (am_server && am_sender) {
47 write_longint(f,read_total());
48 write_longint(f,write_total());
49 write_longint(f,total_size);
50 write_flush(f);
51 return;
52 }
c627d613 53
7a6421fa
AT
54 if (am_sender) {
55 in = read_total();
56 out = write_total();
57 tsize = total_size;
58 } else {
7a6421fa 59 out = read_longint(f);
d7ff63cf 60 in = read_longint(f);
7a6421fa
AT
61 tsize = read_longint(f);
62 }
63
64 printf("wrote %.0f bytes read %.0f bytes %.2f bytes/sec\n",
65 (double)out,(double)in,(in+out)/(0.5 + (t-starttime)));
66 printf("total size is %.0f speedup is %.2f\n",
67 (double)tsize,(1.0*tsize)/(in+out));
c627d613
AT
68}
69
70
e3cd198f 71static int do_cmd(char *cmd,char *machine,char *user,char *path,int *f_in,int *f_out)
c627d613 72{
366345fe
AT
73 char *args[100];
74 int i,argc=0, ret;
75 char *tok,*dir=NULL;
7a6421fa
AT
76 extern int local_server;
77 extern char *rsync_path;
366345fe
AT
78
79 if (!local_server) {
80 if (!cmd)
81 cmd = getenv(RSYNC_RSH_ENV);
82 if (!cmd)
83 cmd = RSYNC_RSH;
84 cmd = strdup(cmd);
85 if (!cmd)
86 goto oom;
87
88 for (tok=strtok(cmd," ");tok;tok=strtok(NULL," ")) {
89 args[argc++] = tok;
90 }
c627d613 91
7b8356d0 92#if HAVE_REMSH
366345fe
AT
93 /* remsh (on HPUX) takes the arguments the other way around */
94 args[argc++] = machine;
95 if (user) {
96 args[argc++] = "-l";
97 args[argc++] = user;
98 }
7b8356d0 99#else
366345fe
AT
100 if (user) {
101 args[argc++] = "-l";
102 args[argc++] = user;
103 }
104 args[argc++] = machine;
7b8356d0 105#endif
c627d613 106
366345fe 107 args[argc++] = rsync_path;
c627d613 108
366345fe
AT
109 server_options(args,&argc);
110 }
c627d613 111
366345fe 112 args[argc++] = ".";
76076c4b 113
366345fe
AT
114 if (path && *path)
115 args[argc++] = path;
c627d613 116
366345fe 117 args[argc] = NULL;
c627d613 118
366345fe 119 if (verbose > 3) {
9486289c 120 rprintf(FINFO,"cmd=");
366345fe 121 for (i=0;i<argc;i++)
9486289c
AT
122 rprintf(FINFO,"%s ",args[i]);
123 rprintf(FINFO,"\n");
366345fe
AT
124 }
125
126 if (local_server) {
127 ret = local_child(argc, args, f_in, f_out);
128 } else {
129 ret = piped_child(args,f_in,f_out);
130 }
c627d613 131
366345fe 132 if (dir) free(dir);
82306bf6 133
366345fe 134 return ret;
c627d613
AT
135
136oom:
366345fe
AT
137 out_of_memory("do_cmd");
138 return 0; /* not reached */
c627d613
AT
139}
140
141
142
143
144static char *get_local_name(struct file_list *flist,char *name)
145{
7a6421fa
AT
146 STRUCT_STAT st;
147 extern int orig_umask;
c627d613 148
bcacc18b 149 if (do_stat(name,&st) == 0) {
c627d613
AT
150 if (S_ISDIR(st.st_mode)) {
151 if (chdir(name) != 0) {
9486289c 152 rprintf(FERROR,"chdir %s : %s (1)\n",name,strerror(errno));
34ccb63e 153 exit_cleanup(1);
c627d613
AT
154 }
155 return NULL;
156 }
157 if (flist->count > 1) {
9486289c 158 rprintf(FERROR,"ERROR: destination must be a directory when copying more than 1 file\n");
34ccb63e 159 exit_cleanup(1);
c627d613
AT
160 }
161 return name;
162 }
163
164 if (flist->count == 1)
165 return name;
166
167 if (!name)
168 return NULL;
169
1b2d733a 170 if (do_mkdir(name,0777 & ~orig_umask) != 0) {
9486289c 171 rprintf(FERROR,"mkdir %s : %s (1)\n",name,strerror(errno));
34ccb63e 172 exit_cleanup(1);
94481d91 173 } else {
9486289c 174 rprintf(FINFO,"created directory %s\n",name);
c627d613
AT
175 }
176
177 if (chdir(name) != 0) {
9486289c 178 rprintf(FERROR,"chdir %s : %s (2)\n",name,strerror(errno));
34ccb63e 179 exit_cleanup(1);
c627d613
AT
180 }
181
182 return NULL;
183}
184
185
186
187
9486289c 188static void do_server_sender(int f_in, int f_out, int argc,char *argv[])
c627d613 189{
7a6421fa
AT
190 int i;
191 struct file_list *flist;
192 char *dir = argv[0];
193 extern int relative_paths;
7a6421fa 194 extern int recurse;
c627d613 195
7a6421fa
AT
196 if (verbose > 2)
197 rprintf(FINFO,"server_sender starting pid=%d\n",(int)getpid());
c627d613 198
7a6421fa
AT
199 if (!relative_paths && chdir(dir) != 0) {
200 rprintf(FERROR,"chdir %s: %s (3)\n",dir,strerror(errno));
201 exit_cleanup(1);
202 }
203 argc--;
204 argv++;
c627d613 205
7a6421fa
AT
206 if (strcmp(dir,".")) {
207 int l = strlen(dir);
208 if (strcmp(dir,"/") == 0)
209 l = 0;
210 for (i=0;i<argc;i++)
211 argv[i] += l+1;
212 }
c627d613 213
7a6421fa
AT
214 if (argc == 0 && recurse) {
215 argc=1;
216 argv--;
217 argv[0] = ".";
218 }
219
7a6421fa
AT
220 flist = send_file_list(f_out,argc,argv);
221 send_files(flist,f_out,f_in);
222 report(f_out);
223 exit_cleanup(0);
c627d613
AT
224}
225
226
dc5ddbcc
AT
227static int do_recv(int f_in,int f_out,struct file_list *flist,char *local_name)
228{
d186eb1a
AT
229 int pid;
230 int status=0;
231 int recv_pipe[2];
232 extern int preserve_hard_links;
233 extern int am_daemon;
dc5ddbcc 234
d186eb1a
AT
235 if (preserve_hard_links)
236 init_hard_links(flist);
dc5ddbcc 237
d186eb1a
AT
238 if (pipe(recv_pipe) < 0) {
239 rprintf(FERROR,"pipe failed in do_recv\n");
240 exit(1);
241 }
c6e7fcb4
AT
242
243
d186eb1a 244 if ((pid=do_fork()) == 0) {
6c29af22 245 close(recv_pipe[0]);
d186eb1a
AT
246 recv_files(f_in,flist,local_name,recv_pipe[1]);
247 if (am_daemon) report(-1);
248 exit_cleanup(0);
249 }
dc5ddbcc 250
6c29af22 251 close(recv_pipe[1]);
d186eb1a 252 generate_files(f_out,flist,local_name,recv_pipe[0]);
dc5ddbcc 253
d186eb1a 254 waitpid(pid, &status, 0);
dc5ddbcc 255
d186eb1a 256 return status;
dc5ddbcc
AT
257}
258
c627d613 259
9486289c 260static void do_server_recv(int f_in, int f_out, int argc,char *argv[])
c627d613 261{
7a6421fa
AT
262 int status;
263 struct file_list *flist;
264 char *local_name=NULL;
265 char *dir = NULL;
266 extern int delete_mode;
267 extern int am_daemon;
f0fca04e 268
7a6421fa
AT
269 if (verbose > 2)
270 rprintf(FINFO,"server_recv(%d) starting pid=%d\n",argc,(int)getpid());
271
7a6421fa
AT
272 if (argc > 0) {
273 dir = argv[0];
274 argc--;
275 argv++;
276 if (!am_daemon && chdir(dir) != 0) {
277 rprintf(FERROR,"chdir %s : %s (4)\n",
278 dir,strerror(errno));
279 exit_cleanup(1);
280 }
281 }
c627d613 282
7a6421fa
AT
283 if (delete_mode)
284 recv_exclude_list(f_in);
c627d613 285
7a6421fa
AT
286 flist = recv_file_list(f_in);
287 if (!flist || flist->count == 0) {
288 rprintf(FERROR,"nothing to do\n");
289 exit_cleanup(1);
290 }
291
292 if (argc > 0) {
293 if (strcmp(dir,".")) {
294 argv[0] += strlen(dir);
295 if (argv[0][0] == '/') argv[0]++;
296 }
297 local_name = get_local_name(flist,argv[0]);
298 }
c627d613 299
7a6421fa
AT
300 status = do_recv(f_in,f_out,flist,local_name);
301 exit_cleanup(status);
c627d613
AT
302}
303
304
9486289c 305void start_server(int f_in, int f_out, int argc, char *argv[])
366345fe 306{
7a6421fa
AT
307 extern int cvs_exclude;
308 extern int am_sender;
309
310 setup_protocol(f_out, f_in);
366345fe 311
7a6421fa
AT
312 if (am_sender) {
313 recv_exclude_list(f_in);
314 if (cvs_exclude)
315 add_cvs_excludes();
316 do_server_sender(f_in, f_out, argc, argv);
317 } else {
318 do_server_recv(f_in, f_out, argc, argv);
319 }
320 exit_cleanup(0);
366345fe
AT
321}
322
3591c066 323int client_run(int f_in, int f_out, int pid, int argc, char *argv[])
9486289c
AT
324{
325 struct file_list *flist;
326 int status = 0, status2 = 0;
327 char *local_name = NULL;
7a6421fa 328 extern int am_sender;
9486289c
AT
329
330 setup_protocol(f_out,f_in);
331
332 if (am_sender) {
7a6421fa
AT
333 extern int cvs_exclude;
334 extern int delete_mode;
9486289c
AT
335 if (cvs_exclude)
336 add_cvs_excludes();
337 if (delete_mode)
338 send_exclude_list(f_out);
339 flist = send_file_list(f_out,argc,argv);
340 if (verbose > 3)
341 rprintf(FINFO,"file list sent\n");
342 send_files(flist,f_out,f_in);
343 if (pid != -1) {
344 if (verbose > 3)
345 rprintf(FINFO,"waiting on %d\n",pid);
346 waitpid(pid, &status, 0);
347 }
348 report(-1);
349 exit_cleanup(status);
350 }
351
352 send_exclude_list(f_out);
353
354 flist = recv_file_list(f_in);
355 if (!flist || flist->count == 0) {
356 rprintf(FINFO,"nothing to do\n");
357 exit_cleanup(0);
358 }
359
360 local_name = get_local_name(flist,argv[0]);
361
362 status2 = do_recv(f_in,f_out,flist,local_name);
363
364 report(f_in);
365
366 if (pid != -1) {
367 waitpid(pid, &status, 0);
368 }
369
370 return status | status2;
371}
372
373
5d6bcd44
AT
374int start_client(int argc, char *argv[])
375{
376 char *p;
377 char *shell_machine = NULL;
378 char *shell_path = NULL;
379 char *shell_user = NULL;
9486289c 380 int pid;
5d6bcd44 381 int f_in,f_out;
7a6421fa
AT
382 extern int local_server;
383 extern int am_sender;
384 extern char *shell_cmd;
5d6bcd44
AT
385
386 p = strchr(argv[0],':');
387
388 if (p) {
9486289c
AT
389 if (p[1] == ':') {
390 *p = 0;
391 return start_socket_client(argv[0], p+2, argc-1, argv+1);
392 }
3591c066
AT
393
394 if (argc < 2) {
395 usage(FERROR);
396 exit_cleanup(1);
397 }
398
5d6bcd44
AT
399 am_sender = 0;
400 *p = 0;
401 shell_machine = argv[0];
402 shell_path = p+1;
403 argc--;
404 argv++;
405 } else {
406 am_sender = 1;
9486289c 407
5d6bcd44
AT
408 p = strchr(argv[argc-1],':');
409 if (!p) {
410 local_server = 1;
9486289c
AT
411 } else if (p[1] == ':') {
412 *p = 0;
413 return start_socket_client(argv[argc-1], p+2, argc-1, argv);
5d6bcd44 414 }
3591c066
AT
415
416 if (argc < 2) {
417 usage(FERROR);
418 exit_cleanup(1);
419 }
9486289c 420
5d6bcd44
AT
421 if (local_server) {
422 shell_machine = NULL;
423 shell_path = argv[argc-1];
424 } else {
425 *p = 0;
426 shell_machine = argv[argc-1];
427 shell_path = p+1;
428 }
429 argc--;
430 }
431
432 if (shell_machine) {
433 p = strchr(shell_machine,'@');
434 if (p) {
435 *p = 0;
436 shell_user = shell_machine;
437 shell_machine = p+1;
438 }
439 }
440
441 if (verbose > 3) {
9486289c 442 rprintf(FINFO,"cmd=%s machine=%s user=%s path=%s\n",
5d6bcd44
AT
443 shell_cmd?shell_cmd:"",
444 shell_machine?shell_machine:"",
445 shell_user?shell_user:"",
446 shell_path?shell_path:"");
447 }
448
449 if (!am_sender && argc != 1) {
450 usage(FERROR);
451 exit_cleanup(1);
452 }
453
454 pid = do_cmd(shell_cmd,shell_machine,shell_user,shell_path,&f_in,&f_out);
455
5d6bcd44 456#if HAVE_SETLINEBUF
9486289c
AT
457 setlinebuf(stdout);
458 setlinebuf(stderr);
5d6bcd44 459#endif
9486289c
AT
460
461 return client_run(f_in, f_out, pid, argc, argv);
5d6bcd44
AT
462}
463
366345fe 464
82306bf6
AT
465RETSIGTYPE sigusr1_handler(int val) {
466 exit_cleanup(1);
467}
468
5d6bcd44 469int main(int argc,char *argv[])
7a6421fa
AT
470{
471 extern int am_root;
472 extern int orig_umask;
473 extern int dry_run;
474 extern int am_daemon;
475 extern int am_server;
5d6bcd44 476
7a6421fa 477 signal(SIGUSR1, sigusr1_handler);
5d6bcd44 478
7a6421fa
AT
479 starttime = time(NULL);
480 am_root = (getuid() == 0);
c627d613 481
df5e03da
AT
482 if (argc < 2) {
483 usage(FERROR);
484 exit(1);
485 }
486
7a6421fa
AT
487 /* we set a 0 umask so that correct file permissions can be
488 carried across */
489 orig_umask = (int)umask(0);
5d6bcd44 490
7a6421fa 491 parse_arguments(argc, argv);
5d6bcd44 492
7a6421fa
AT
493 argc -= optind;
494 argv += optind;
495 optind = 0;
c627d613 496
7a6421fa
AT
497 signal(SIGCHLD,SIG_IGN);
498 signal(SIGINT,SIGNAL_CAST sig_int);
499 signal(SIGPIPE,SIGNAL_CAST sig_int);
500 signal(SIGHUP,SIGNAL_CAST sig_int);
6b83141d 501
7a6421fa
AT
502 if (am_daemon) {
503 return daemon_main();
504 }
f0fca04e 505
08ac228f
AT
506 if (argc < 1) {
507 usage(FERROR);
508 exit(1);
509 }
510
7a6421fa
AT
511 if (dry_run)
512 verbose = MAX(verbose,1);
c627d613 513
cbbe4892 514#ifndef SUPPORT_LINKS
7a6421fa
AT
515 if (!am_server && preserve_links) {
516 rprintf(FERROR,"ERROR: symbolic links not supported\n");
517 exit_cleanup(1);
518 }
cbbe4892
AT
519#endif
520
7a6421fa
AT
521 if (am_server) {
522 start_server(STDIN_FILENO, STDOUT_FILENO, argc, argv);
523 }
c627d613 524
7a6421fa 525 return start_client(argc, argv);
c627d613 526}
82306bf6 527