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);
40 rprintf(FINFO, "wrote %.0f bytes read %.0f bytes total size %.0f\n",
41 (double)write_total(),(double)read_total(),
45 if (am_server && am_sender) {
46 write_longint(f,read_total());
47 write_longint(f,write_total());
48 write_longint(f,total_size);
58 out = read_longint(f);
60 tsize = read_longint(f);
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));
70 static int do_cmd(char *cmd,char *machine,char *user,char *path,int *f_in,int *f_out)
75 extern int local_server;
76 extern char *rsync_path;
80 cmd = getenv(RSYNC_RSH_ENV);
87 for (tok=strtok(cmd," ");tok;tok=strtok(NULL," ")) {
92 /* remsh (on HPUX) takes the arguments the other way around */
93 args[argc++] = machine;
103 args[argc++] = machine;
106 args[argc++] = rsync_path;
108 server_options(args,&argc);
119 rprintf(FINFO,"cmd=");
121 rprintf(FINFO,"%s ",args[i]);
126 ret = local_child(argc, args, f_in, f_out);
128 ret = piped_child(args,f_in,f_out);
136 out_of_memory("do_cmd");
137 return 0; /* not reached */
143 static char *get_local_name(struct file_list *flist,char *name)
146 extern int orig_umask;
148 if (do_stat(name,&st) == 0) {
149 if (S_ISDIR(st.st_mode)) {
150 if (chdir(name) != 0) {
151 rprintf(FERROR,"chdir %s : %s (1)\n",name,strerror(errno));
156 if (flist->count > 1) {
157 rprintf(FERROR,"ERROR: destination must be a directory when copying more than 1 file\n");
163 if (flist->count == 1)
169 if (do_mkdir(name,0777 & ~orig_umask) != 0) {
170 rprintf(FERROR,"mkdir %s : %s (1)\n",name,strerror(errno));
173 rprintf(FINFO,"created directory %s\n",name);
176 if (chdir(name) != 0) {
177 rprintf(FERROR,"chdir %s : %s (2)\n",name,strerror(errno));
187 static void do_server_sender(int f_in, int f_out, int argc,char *argv[])
190 struct file_list *flist;
192 extern int relative_paths;
196 rprintf(FINFO,"server_sender starting pid=%d\n",(int)getpid());
198 if (!relative_paths && chdir(dir) != 0) {
199 rprintf(FERROR,"chdir %s: %s (3)\n",dir,strerror(errno));
205 if (strcmp(dir,".")) {
207 if (strcmp(dir,"/") == 0)
213 if (argc == 0 && recurse) {
219 flist = send_file_list(f_out,argc,argv);
220 send_files(flist,f_out,f_in);
226 static int do_recv(int f_in,int f_out,struct file_list *flist,char *local_name)
231 extern int preserve_hard_links;
233 if (preserve_hard_links)
234 init_hard_links(flist);
236 if (pipe(recv_pipe) < 0) {
237 rprintf(FERROR,"pipe failed in do_recv\n");
242 if ((pid=do_fork()) == 0) {
243 recv_files(f_in,flist,local_name,recv_pipe[1]);
245 rprintf(FINFO,"receiver read %ld\n",(long)read_total());
249 generate_files(f_out,flist,local_name,recv_pipe[0]);
251 waitpid(pid, &status, 0);
257 static void do_server_recv(int f_in, int f_out, int argc,char *argv[])
260 struct file_list *flist;
261 char *local_name=NULL;
263 extern int delete_mode;
264 extern int am_daemon;
267 rprintf(FINFO,"server_recv(%d) starting pid=%d\n",argc,(int)getpid());
273 if (!am_daemon && chdir(dir) != 0) {
274 rprintf(FERROR,"chdir %s : %s (4)\n",
275 dir,strerror(errno));
281 recv_exclude_list(f_in);
283 flist = recv_file_list(f_in);
284 if (!flist || flist->count == 0) {
285 rprintf(FERROR,"nothing to do\n");
290 if (strcmp(dir,".")) {
291 argv[0] += strlen(dir);
292 if (argv[0][0] == '/') argv[0]++;
294 local_name = get_local_name(flist,argv[0]);
297 status = do_recv(f_in,f_out,flist,local_name);
298 exit_cleanup(status);
302 void start_server(int f_in, int f_out, int argc, char *argv[])
304 extern int cvs_exclude;
305 extern int am_sender;
307 setup_protocol(f_out, f_in);
310 recv_exclude_list(f_in);
313 do_server_sender(f_in, f_out, argc, argv);
315 do_server_recv(f_in, f_out, argc, argv);
320 int client_run(int f_in, int f_out, int pid, int argc, char *argv[])
322 struct file_list *flist;
323 int status = 0, status2 = 0;
324 char *local_name = NULL;
325 extern int am_sender;
327 setup_protocol(f_out,f_in);
330 extern int cvs_exclude;
331 extern int delete_mode;
335 send_exclude_list(f_out);
336 flist = send_file_list(f_out,argc,argv);
338 rprintf(FINFO,"file list sent\n");
339 send_files(flist,f_out,f_in);
342 rprintf(FINFO,"waiting on %d\n",pid);
343 waitpid(pid, &status, 0);
346 exit_cleanup(status);
349 send_exclude_list(f_out);
351 flist = recv_file_list(f_in);
352 if (!flist || flist->count == 0) {
353 rprintf(FINFO,"nothing to do\n");
357 local_name = get_local_name(flist,argv[0]);
359 status2 = do_recv(f_in,f_out,flist,local_name);
364 waitpid(pid, &status, 0);
367 return status | status2;
371 int start_client(int argc, char *argv[])
374 char *shell_machine = NULL;
375 char *shell_path = NULL;
376 char *shell_user = NULL;
379 extern int local_server;
380 extern int am_sender;
381 extern char *shell_cmd;
383 p = strchr(argv[0],':');
388 return start_socket_client(argv[0], p+2, argc-1, argv+1);
398 shell_machine = argv[0];
405 p = strchr(argv[argc-1],':');
408 } else if (p[1] == ':') {
410 return start_socket_client(argv[argc-1], p+2, argc-1, argv);
419 shell_machine = NULL;
420 shell_path = argv[argc-1];
423 shell_machine = argv[argc-1];
430 p = strchr(shell_machine,'@');
433 shell_user = shell_machine;
439 rprintf(FINFO,"cmd=%s machine=%s user=%s path=%s\n",
440 shell_cmd?shell_cmd:"",
441 shell_machine?shell_machine:"",
442 shell_user?shell_user:"",
443 shell_path?shell_path:"");
446 if (!am_sender && argc != 1) {
451 pid = do_cmd(shell_cmd,shell_machine,shell_user,shell_path,&f_in,&f_out);
458 return client_run(f_in, f_out, pid, argc, argv);
462 RETSIGTYPE sigusr1_handler(int val) {
466 int main(int argc,char *argv[])
469 extern int orig_umask;
471 extern int am_daemon;
472 extern int am_server;
474 signal(SIGUSR1, sigusr1_handler);
476 starttime = time(NULL);
477 am_root = (getuid() == 0);
484 /* we set a 0 umask so that correct file permissions can be
486 orig_umask = (int)umask(0);
488 parse_arguments(argc, argv);
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);
500 return daemon_main();
504 verbose = MAX(verbose,1);
506 #ifndef SUPPORT_LINKS
507 if (!am_server && preserve_links) {
508 rprintf(FERROR,"ERROR: symbolic links not supported\n");
514 start_server(STDIN_FILENO, STDOUT_FILENO, argc, argv);
517 return start_client(argc, argv);