2 Copyright (C) Andrew Tridgell 1996
3 Copyright (C) Paul Mackerras 1996
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.
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.
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.
25 extern int csum_length;
29 static void report(int f)
32 time_t t = time(NULL);
38 syslog(LOG_INFO,"wrote %.0f bytes read %.0f bytes total size %.0f\n",
39 (double)write_total(),(double)read_total(),
41 if (f == -1 || !am_sender) return;
46 if (am_server && !am_sender) return;
48 if (am_server && am_sender) {
49 write_longint(f,read_total());
50 write_longint(f,write_total());
51 write_longint(f,total_size);
61 out = read_longint(f);
63 tsize = read_longint(f);
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));
73 static int do_cmd(char *cmd,char *machine,char *user,char *path,int *f_in,int *f_out)
78 extern int local_server;
79 extern char *rsync_path;
83 cmd = getenv(RSYNC_RSH_ENV);
90 for (tok=strtok(cmd," ");tok;tok=strtok(NULL," ")) {
95 /* remsh (on HPUX) takes the arguments the other way around */
96 args[argc++] = machine;
106 args[argc++] = machine;
109 args[argc++] = rsync_path;
111 server_options(args,&argc);
122 rprintf(FINFO,"cmd=");
124 rprintf(FINFO,"%s ",args[i]);
129 ret = local_child(argc, args, f_in, f_out);
131 ret = piped_child(args,f_in,f_out);
139 out_of_memory("do_cmd");
140 return 0; /* not reached */
146 static char *get_local_name(struct file_list *flist,char *name)
149 extern int orig_umask;
151 if (do_stat(name,&st) == 0) {
152 if (S_ISDIR(st.st_mode)) {
153 if (chdir(name) != 0) {
154 rprintf(FERROR,"chdir %s : %s (1)\n",name,strerror(errno));
159 if (flist->count > 1) {
160 rprintf(FERROR,"ERROR: destination must be a directory when copying more than 1 file\n");
166 if (flist->count == 1)
172 if (do_mkdir(name,0777 & ~orig_umask) != 0) {
173 rprintf(FERROR,"mkdir %s : %s (1)\n",name,strerror(errno));
176 rprintf(FINFO,"created directory %s\n",name);
179 if (chdir(name) != 0) {
180 rprintf(FERROR,"chdir %s : %s (2)\n",name,strerror(errno));
190 static void do_server_sender(int f_in, int f_out, int argc,char *argv[])
193 struct file_list *flist;
195 extern int relative_paths;
199 rprintf(FINFO,"server_sender starting pid=%d\n",(int)getpid());
201 if (!relative_paths && chdir(dir) != 0) {
202 rprintf(FERROR,"chdir %s: %s (3)\n",dir,strerror(errno));
208 if (strcmp(dir,".")) {
210 if (strcmp(dir,"/") == 0)
216 if (argc == 0 && recurse) {
222 flist = send_file_list(f_out,argc,argv);
223 if (!flist || flist->count == 0) {
227 send_files(flist,f_out,f_in);
234 static int do_recv(int f_in,int f_out,struct file_list *flist,char *local_name)
239 extern int preserve_hard_links;
241 if (preserve_hard_links)
242 init_hard_links(flist);
244 if (pipe(recv_pipe) < 0) {
245 rprintf(FERROR,"pipe failed in do_recv\n");
251 if ((pid=do_fork()) == 0) {
253 if (f_in != f_out) close(f_out);
255 recv_files(f_in,flist,local_name,recv_pipe[1]);
259 rprintf(FINFO,"do_recv waiting on %d\n",pid);
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]);
271 waitpid(pid, &status, 0);
276 static void do_server_recv(int f_in, int f_out, int argc,char *argv[])
279 struct file_list *flist;
280 char *local_name=NULL;
282 extern int delete_mode;
283 extern int am_daemon;
286 rprintf(FINFO,"server_recv(%d) starting pid=%d\n",argc,(int)getpid());
292 if (!am_daemon && chdir(dir) != 0) {
293 rprintf(FERROR,"chdir %s : %s (4)\n",
294 dir,strerror(errno));
300 recv_exclude_list(f_in);
302 flist = recv_file_list(f_in);
303 if (!flist || flist->count == 0) {
304 rprintf(FERROR,"server_recv: nothing to do\n");
309 if (strcmp(dir,".")) {
310 argv[0] += strlen(dir);
311 if (argv[0][0] == '/') argv[0]++;
313 local_name = get_local_name(flist,argv[0]);
316 status = do_recv(f_in,f_out,flist,local_name);
317 exit_cleanup(status);
321 void start_server(int f_in, int f_out, int argc, char *argv[])
323 extern int cvs_exclude;
324 extern int am_sender;
326 setup_protocol(f_out, f_in);
329 recv_exclude_list(f_in);
332 do_server_sender(f_in, f_out, argc, argv);
334 do_server_recv(f_in, f_out, argc, argv);
339 int client_run(int f_in, int f_out, int pid, int argc, char *argv[])
341 struct file_list *flist;
342 int status = 0, status2 = 0;
343 char *local_name = NULL;
344 extern int am_sender;
346 setup_protocol(f_out,f_in);
349 extern int cvs_exclude;
350 extern int delete_mode;
354 send_exclude_list(f_out);
355 flist = send_file_list(f_out,argc,argv);
357 rprintf(FINFO,"file list sent\n");
358 send_files(flist,f_out,f_in);
361 rprintf(FINFO,"client_run waiting on %d\n",pid);
363 waitpid(pid, &status, 0);
366 exit_cleanup(status);
369 send_exclude_list(f_out);
371 flist = recv_file_list(f_in);
372 if (!flist || flist->count == 0) {
373 rprintf(FINFO,"client: nothing to do\n");
377 local_name = get_local_name(flist,argv[0]);
379 status2 = do_recv(f_in,f_out,flist,local_name);
383 rprintf(FINFO,"client_run2 waiting on %d\n",pid);
385 waitpid(pid, &status, 0);
388 return status | status2;
392 int start_client(int argc, char *argv[])
395 char *shell_machine = NULL;
396 char *shell_path = NULL;
397 char *shell_user = NULL;
400 extern int local_server;
401 extern int am_sender;
402 extern char *shell_cmd;
404 p = strchr(argv[0],':');
409 return start_socket_client(argv[0], p+2, argc-1, argv+1);
419 shell_machine = argv[0];
426 p = strchr(argv[argc-1],':');
429 } else if (p[1] == ':') {
431 return start_socket_client(argv[argc-1], p+2, argc-1, argv);
440 shell_machine = NULL;
441 shell_path = argv[argc-1];
444 shell_machine = argv[argc-1];
451 p = strchr(shell_machine,'@');
454 shell_user = shell_machine;
460 rprintf(FINFO,"cmd=%s machine=%s user=%s path=%s\n",
461 shell_cmd?shell_cmd:"",
462 shell_machine?shell_machine:"",
463 shell_user?shell_user:"",
464 shell_path?shell_path:"");
467 if (!am_sender && argc != 1) {
472 pid = do_cmd(shell_cmd,shell_machine,shell_user,shell_path,&f_in,&f_out);
479 return client_run(f_in, f_out, pid, argc, argv);
483 RETSIGTYPE sigusr1_handler(int val) {
487 int main(int argc,char *argv[])
490 extern int orig_umask;
492 extern int am_daemon;
493 extern int am_server;
495 signal(SIGUSR1, sigusr1_handler);
497 starttime = time(NULL);
498 am_root = (getuid() == 0);
505 /* we set a 0 umask so that correct file permissions can be
507 orig_umask = (int)umask(0);
509 parse_arguments(argc, argv);
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);
521 return daemon_main();
530 verbose = MAX(verbose,1);
532 #ifndef SUPPORT_LINKS
533 if (!am_server && preserve_links) {
534 rprintf(FERROR,"ERROR: symbolic links not supported\n");
540 start_server(STDIN_FILENO, STDOUT_FILENO, argc, argv);
543 return start_client(argc, argv);