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