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 rprintf(FINFO, "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) {
47 write_longint(f,read_total());
48 write_longint(f,write_total());
49 write_longint(f,total_size);
59 out = read_longint(f);
61 tsize = read_longint(f);
64 printf("wrote %.0f bytes read %.0f bytes %.2f bytes/sec\n",
65 (double)out,(double)in,(in+out)/(0.5 + (t-starttime)));
66 printf("total size is %.0f speedup is %.2f\n",
67 (double)tsize,(1.0*tsize)/(in+out));
71 static int do_cmd(char *cmd,char *machine,char *user,char *path,int *f_in,int *f_out)
76 extern int local_server;
77 extern char *rsync_path;
81 cmd = getenv(RSYNC_RSH_ENV);
88 for (tok=strtok(cmd," ");tok;tok=strtok(NULL," ")) {
93 /* remsh (on HPUX) takes the arguments the other way around */
94 args[argc++] = machine;
104 args[argc++] = machine;
107 args[argc++] = rsync_path;
109 server_options(args,&argc);
120 rprintf(FINFO,"cmd=");
122 rprintf(FINFO,"%s ",args[i]);
127 ret = local_child(argc, args, f_in, f_out);
129 ret = piped_child(args,f_in,f_out);
137 out_of_memory("do_cmd");
138 return 0; /* not reached */
144 static char *get_local_name(struct file_list *flist,char *name)
147 extern int orig_umask;
149 if (do_stat(name,&st) == 0) {
150 if (S_ISDIR(st.st_mode)) {
151 if (chdir(name) != 0) {
152 rprintf(FERROR,"chdir %s : %s (1)\n",name,strerror(errno));
157 if (flist->count > 1) {
158 rprintf(FERROR,"ERROR: destination must be a directory when copying more than 1 file\n");
164 if (flist->count == 1)
170 if (do_mkdir(name,0777 & ~orig_umask) != 0) {
171 rprintf(FERROR,"mkdir %s : %s (1)\n",name,strerror(errno));
174 rprintf(FINFO,"created directory %s\n",name);
177 if (chdir(name) != 0) {
178 rprintf(FERROR,"chdir %s : %s (2)\n",name,strerror(errno));
188 static void do_server_sender(int f_in, int f_out, int argc,char *argv[])
191 struct file_list *flist;
193 extern int relative_paths;
197 rprintf(FINFO,"server_sender starting pid=%d\n",(int)getpid());
199 if (!relative_paths && chdir(dir) != 0) {
200 rprintf(FERROR,"chdir %s: %s (3)\n",dir,strerror(errno));
206 if (strcmp(dir,".")) {
208 if (strcmp(dir,"/") == 0)
214 if (argc == 0 && recurse) {
220 flist = send_file_list(f_out,argc,argv);
221 send_files(flist,f_out,f_in);
227 static int do_recv(int f_in,int f_out,struct file_list *flist,char *local_name)
232 extern int preserve_hard_links;
234 if (preserve_hard_links)
235 init_hard_links(flist);
237 if (pipe(recv_pipe) < 0) {
238 rprintf(FERROR,"pipe failed in do_recv\n");
243 if ((pid=do_fork()) == 0) {
244 recv_files(f_in,flist,local_name,recv_pipe[1]);
246 rprintf(FINFO,"receiver read %ld\n",(long)read_total());
250 generate_files(f_out,flist,local_name,recv_pipe[0]);
252 waitpid(pid, &status, 0);
258 static void do_server_recv(int f_in, int f_out, int argc,char *argv[])
261 struct file_list *flist;
262 char *local_name=NULL;
264 extern int delete_mode;
265 extern int am_daemon;
268 rprintf(FINFO,"server_recv(%d) starting pid=%d\n",argc,(int)getpid());
274 if (!am_daemon && chdir(dir) != 0) {
275 rprintf(FERROR,"chdir %s : %s (4)\n",
276 dir,strerror(errno));
282 recv_exclude_list(f_in);
284 flist = recv_file_list(f_in);
285 if (!flist || flist->count == 0) {
286 rprintf(FERROR,"nothing to do\n");
291 if (strcmp(dir,".")) {
292 argv[0] += strlen(dir);
293 if (argv[0][0] == '/') argv[0]++;
295 local_name = get_local_name(flist,argv[0]);
298 status = do_recv(f_in,f_out,flist,local_name);
300 exit_cleanup(status);
304 void start_server(int f_in, int f_out, int argc, char *argv[])
306 extern int cvs_exclude;
307 extern int am_sender;
309 setup_protocol(f_out, f_in);
312 recv_exclude_list(f_in);
315 do_server_sender(f_in, f_out, argc, argv);
317 do_server_recv(f_in, f_out, argc, argv);
322 int client_run(int f_in, int f_out, int pid, int argc, char *argv[])
324 struct file_list *flist;
325 int status = 0, status2 = 0;
326 char *local_name = NULL;
327 extern int am_sender;
329 setup_protocol(f_out,f_in);
332 extern int cvs_exclude;
333 extern int delete_mode;
337 send_exclude_list(f_out);
338 flist = send_file_list(f_out,argc,argv);
340 rprintf(FINFO,"file list sent\n");
341 send_files(flist,f_out,f_in);
344 rprintf(FINFO,"waiting on %d\n",pid);
345 waitpid(pid, &status, 0);
348 exit_cleanup(status);
351 send_exclude_list(f_out);
353 flist = recv_file_list(f_in);
354 if (!flist || flist->count == 0) {
355 rprintf(FINFO,"nothing to do\n");
359 local_name = get_local_name(flist,argv[0]);
361 status2 = do_recv(f_in,f_out,flist,local_name);
366 waitpid(pid, &status, 0);
369 return status | status2;
373 int start_client(int argc, char *argv[])
376 char *shell_machine = NULL;
377 char *shell_path = NULL;
378 char *shell_user = NULL;
381 extern int local_server;
382 extern int am_sender;
383 extern char *shell_cmd;
385 p = strchr(argv[0],':');
390 return start_socket_client(argv[0], p+2, argc-1, argv+1);
400 shell_machine = argv[0];
407 p = strchr(argv[argc-1],':');
410 } else if (p[1] == ':') {
412 return start_socket_client(argv[argc-1], p+2, argc-1, argv);
421 shell_machine = NULL;
422 shell_path = argv[argc-1];
425 shell_machine = argv[argc-1];
432 p = strchr(shell_machine,'@');
435 shell_user = shell_machine;
441 rprintf(FINFO,"cmd=%s machine=%s user=%s path=%s\n",
442 shell_cmd?shell_cmd:"",
443 shell_machine?shell_machine:"",
444 shell_user?shell_user:"",
445 shell_path?shell_path:"");
448 if (!am_sender && argc != 1) {
453 pid = do_cmd(shell_cmd,shell_machine,shell_user,shell_path,&f_in,&f_out);
460 return client_run(f_in, f_out, pid, argc, argv);
464 RETSIGTYPE sigusr1_handler(int val) {
468 int main(int argc,char *argv[])
471 extern int orig_umask;
473 extern int am_daemon;
474 extern int am_server;
476 signal(SIGUSR1, sigusr1_handler);
478 starttime = time(NULL);
479 am_root = (getuid() == 0);
486 /* we set a 0 umask so that correct file permissions can be
488 orig_umask = (int)umask(0);
490 parse_arguments(argc, argv);
496 signal(SIGCHLD,SIG_IGN);
497 signal(SIGINT,SIGNAL_CAST sig_int);
498 signal(SIGPIPE,SIGNAL_CAST sig_int);
499 signal(SIGHUP,SIGNAL_CAST sig_int);
502 return daemon_main();
506 verbose = MAX(verbose,1);
508 #ifndef SUPPORT_LINKS
509 if (!am_server && preserve_links) {
510 rprintf(FERROR,"ERROR: symbolic links not supported\n");
516 start_server(STDIN_FILENO, STDOUT_FILENO, argc, argv);
519 return start_client(argc, argv);