preparing for release of 2.0.6
[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;
7a6421fa 193 extern int recurse;
c627d613 194
7a6421fa
AT
195 if (verbose > 2)
196 rprintf(FINFO,"server_sender starting pid=%d\n",(int)getpid());
c627d613 197
7a6421fa
AT
198 if (!relative_paths && chdir(dir) != 0) {
199 rprintf(FERROR,"chdir %s: %s (3)\n",dir,strerror(errno));
200 exit_cleanup(1);
201 }
202 argc--;
203 argv++;
c627d613 204
7a6421fa
AT
205 if (strcmp(dir,".")) {
206 int l = strlen(dir);
207 if (strcmp(dir,"/") == 0)
208 l = 0;
209 for (i=0;i<argc;i++)
210 argv[i] += l+1;
211 }
c627d613 212
7a6421fa
AT
213 if (argc == 0 && recurse) {
214 argc=1;
215 argv--;
216 argv[0] = ".";
217 }
218
7a6421fa
AT
219 flist = send_file_list(f_out,argc,argv);
220 send_files(flist,f_out,f_in);
221 report(f_out);
222 exit_cleanup(0);
c627d613
AT
223}
224
225
dc5ddbcc
AT
226static int do_recv(int f_in,int f_out,struct file_list *flist,char *local_name)
227{
228 int pid;
229 int status=0;
c6e7fcb4 230 int recv_pipe[2];
7a6421fa 231 extern int preserve_hard_links;
dc5ddbcc
AT
232
233 if (preserve_hard_links)
234 init_hard_links(flist);
235
c6e7fcb4 236 if (pipe(recv_pipe) < 0) {
9486289c 237 rprintf(FERROR,"pipe failed in do_recv\n");
c6e7fcb4
AT
238 exit(1);
239 }
240
241
3ba62a83 242 if ((pid=do_fork()) == 0) {
c6e7fcb4 243 recv_files(f_in,flist,local_name,recv_pipe[1]);
dc5ddbcc 244 if (verbose > 2)
9486289c 245 rprintf(FINFO,"receiver read %ld\n",(long)read_total());
dc5ddbcc
AT
246 exit_cleanup(0);
247 }
248
c6e7fcb4 249 generate_files(f_out,flist,local_name,recv_pipe[0]);
dc5ddbcc
AT
250
251 waitpid(pid, &status, 0);
252
253 return status;
254}
255
c627d613 256
9486289c 257static void do_server_recv(int f_in, int f_out, int argc,char *argv[])
c627d613 258{
7a6421fa
AT
259 int status;
260 struct file_list *flist;
261 char *local_name=NULL;
262 char *dir = NULL;
263 extern int delete_mode;
264 extern int am_daemon;
f0fca04e 265
7a6421fa
AT
266 if (verbose > 2)
267 rprintf(FINFO,"server_recv(%d) starting pid=%d\n",argc,(int)getpid());
268
7a6421fa
AT
269 if (argc > 0) {
270 dir = argv[0];
271 argc--;
272 argv++;
273 if (!am_daemon && chdir(dir) != 0) {
274 rprintf(FERROR,"chdir %s : %s (4)\n",
275 dir,strerror(errno));
276 exit_cleanup(1);
277 }
278 }
c627d613 279
7a6421fa
AT
280 if (delete_mode)
281 recv_exclude_list(f_in);
c627d613 282
7a6421fa
AT
283 flist = recv_file_list(f_in);
284 if (!flist || flist->count == 0) {
285 rprintf(FERROR,"nothing to do\n");
286 exit_cleanup(1);
287 }
288
289 if (argc > 0) {
290 if (strcmp(dir,".")) {
291 argv[0] += strlen(dir);
292 if (argv[0][0] == '/') argv[0]++;
293 }
294 local_name = get_local_name(flist,argv[0]);
295 }
c627d613 296
7a6421fa
AT
297 status = do_recv(f_in,f_out,flist,local_name);
298 exit_cleanup(status);
c627d613
AT
299}
300
301
9486289c 302void start_server(int f_in, int f_out, int argc, char *argv[])
366345fe 303{
7a6421fa
AT
304 extern int cvs_exclude;
305 extern int am_sender;
306
307 setup_protocol(f_out, f_in);
366345fe 308
7a6421fa
AT
309 if (am_sender) {
310 recv_exclude_list(f_in);
311 if (cvs_exclude)
312 add_cvs_excludes();
313 do_server_sender(f_in, f_out, argc, argv);
314 } else {
315 do_server_recv(f_in, f_out, argc, argv);
316 }
317 exit_cleanup(0);
366345fe
AT
318}
319
3591c066 320int client_run(int f_in, int f_out, int pid, int argc, char *argv[])
9486289c
AT
321{
322 struct file_list *flist;
323 int status = 0, status2 = 0;
324 char *local_name = NULL;
7a6421fa 325 extern int am_sender;
9486289c
AT
326
327 setup_protocol(f_out,f_in);
328
329 if (am_sender) {
7a6421fa
AT
330 extern int cvs_exclude;
331 extern int delete_mode;
9486289c
AT
332 if (cvs_exclude)
333 add_cvs_excludes();
334 if (delete_mode)
335 send_exclude_list(f_out);
336 flist = send_file_list(f_out,argc,argv);
337 if (verbose > 3)
338 rprintf(FINFO,"file list sent\n");
339 send_files(flist,f_out,f_in);
340 if (pid != -1) {
341 if (verbose > 3)
342 rprintf(FINFO,"waiting on %d\n",pid);
343 waitpid(pid, &status, 0);
344 }
345 report(-1);
346 exit_cleanup(status);
347 }
348
349 send_exclude_list(f_out);
350
351 flist = recv_file_list(f_in);
352 if (!flist || flist->count == 0) {
353 rprintf(FINFO,"nothing to do\n");
354 exit_cleanup(0);
355 }
356
357 local_name = get_local_name(flist,argv[0]);
358
359 status2 = do_recv(f_in,f_out,flist,local_name);
360
361 report(f_in);
362
363 if (pid != -1) {
364 waitpid(pid, &status, 0);
365 }
366
367 return status | status2;
368}
369
370
5d6bcd44
AT
371int start_client(int argc, char *argv[])
372{
373 char *p;
374 char *shell_machine = NULL;
375 char *shell_path = NULL;
376 char *shell_user = NULL;
9486289c 377 int pid;
5d6bcd44 378 int f_in,f_out;
7a6421fa
AT
379 extern int local_server;
380 extern int am_sender;
381 extern char *shell_cmd;
5d6bcd44
AT
382
383 p = strchr(argv[0],':');
384
385 if (p) {
9486289c
AT
386 if (p[1] == ':') {
387 *p = 0;
388 return start_socket_client(argv[0], p+2, argc-1, argv+1);
389 }
3591c066
AT
390
391 if (argc < 2) {
392 usage(FERROR);
393 exit_cleanup(1);
394 }
395
5d6bcd44
AT
396 am_sender = 0;
397 *p = 0;
398 shell_machine = argv[0];
399 shell_path = p+1;
400 argc--;
401 argv++;
402 } else {
403 am_sender = 1;
9486289c 404
5d6bcd44
AT
405 p = strchr(argv[argc-1],':');
406 if (!p) {
407 local_server = 1;
9486289c
AT
408 } else if (p[1] == ':') {
409 *p = 0;
410 return start_socket_client(argv[argc-1], p+2, argc-1, argv);
5d6bcd44 411 }
3591c066
AT
412
413 if (argc < 2) {
414 usage(FERROR);
415 exit_cleanup(1);
416 }
9486289c 417
5d6bcd44
AT
418 if (local_server) {
419 shell_machine = NULL;
420 shell_path = argv[argc-1];
421 } else {
422 *p = 0;
423 shell_machine = argv[argc-1];
424 shell_path = p+1;
425 }
426 argc--;
427 }
428
429 if (shell_machine) {
430 p = strchr(shell_machine,'@');
431 if (p) {
432 *p = 0;
433 shell_user = shell_machine;
434 shell_machine = p+1;
435 }
436 }
437
438 if (verbose > 3) {
9486289c 439 rprintf(FINFO,"cmd=%s machine=%s user=%s path=%s\n",
5d6bcd44
AT
440 shell_cmd?shell_cmd:"",
441 shell_machine?shell_machine:"",
442 shell_user?shell_user:"",
443 shell_path?shell_path:"");
444 }
445
446 if (!am_sender && argc != 1) {
447 usage(FERROR);
448 exit_cleanup(1);
449 }
450
451 pid = do_cmd(shell_cmd,shell_machine,shell_user,shell_path,&f_in,&f_out);
452
5d6bcd44 453#if HAVE_SETLINEBUF
9486289c
AT
454 setlinebuf(stdout);
455 setlinebuf(stderr);
5d6bcd44 456#endif
9486289c
AT
457
458 return client_run(f_in, f_out, pid, argc, argv);
5d6bcd44
AT
459}
460
366345fe 461
82306bf6
AT
462RETSIGTYPE sigusr1_handler(int val) {
463 exit_cleanup(1);
464}
465
5d6bcd44 466int main(int argc,char *argv[])
7a6421fa
AT
467{
468 extern int am_root;
469 extern int orig_umask;
470 extern int dry_run;
471 extern int am_daemon;
472 extern int am_server;
5d6bcd44 473
7a6421fa 474 signal(SIGUSR1, sigusr1_handler);
5d6bcd44 475
7a6421fa
AT
476 starttime = time(NULL);
477 am_root = (getuid() == 0);
c627d613 478
df5e03da
AT
479 if (argc < 2) {
480 usage(FERROR);
481 exit(1);
482 }
483
7a6421fa
AT
484 /* we set a 0 umask so that correct file permissions can be
485 carried across */
486 orig_umask = (int)umask(0);
5d6bcd44 487
7a6421fa 488 parse_arguments(argc, argv);
5d6bcd44 489
7a6421fa
AT
490 argc -= optind;
491 argv += optind;
492 optind = 0;
c627d613 493
7a6421fa
AT
494 signal(SIGCHLD,SIG_IGN);
495 signal(SIGINT,SIGNAL_CAST sig_int);
496 signal(SIGPIPE,SIGNAL_CAST sig_int);
497 signal(SIGHUP,SIGNAL_CAST sig_int);
6b83141d 498
7a6421fa
AT
499 if (am_daemon) {
500 return daemon_main();
501 }
f0fca04e 502
7a6421fa
AT
503 if (dry_run)
504 verbose = MAX(verbose,1);
c627d613 505
cbbe4892 506#ifndef SUPPORT_LINKS
7a6421fa
AT
507 if (!am_server && preserve_links) {
508 rprintf(FERROR,"ERROR: symbolic links not supported\n");
509 exit_cleanup(1);
510 }
cbbe4892
AT
511#endif
512
7a6421fa
AT
513 if (am_server) {
514 start_server(STDIN_FILENO, STDOUT_FILENO, argc, argv);
515 }
c627d613 516
7a6421fa 517 return start_client(argc, argv);
c627d613 518}
82306bf6 519