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