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 if (am_server && am_sender) {
39 write_longint(f,read_total());
40 write_longint(f,write_total());
41 write_longint(f,total_size);
51 out = read_longint(f);
53 tsize = read_longint(f);
56 printf("wrote %.0f bytes read %.0f bytes %.2f bytes/sec\n",
57 (double)out,(double)in,(in+out)/(0.5 + (t-starttime)));
58 printf("total size is %.0f speedup is %.2f\n",
59 (double)tsize,(1.0*tsize)/(in+out));
63 static int do_cmd(char *cmd,char *machine,char *user,char *path,int *f_in,int *f_out)
68 extern int local_server;
69 extern char *rsync_path;
73 cmd = getenv(RSYNC_RSH_ENV);
80 for (tok=strtok(cmd," ");tok;tok=strtok(NULL," ")) {
85 /* remsh (on HPUX) takes the arguments the other way around */
86 args[argc++] = machine;
96 args[argc++] = machine;
99 args[argc++] = rsync_path;
101 server_options(args,&argc);
112 rprintf(FINFO,"cmd=");
114 rprintf(FINFO,"%s ",args[i]);
119 ret = local_child(argc, args, f_in, f_out);
121 ret = piped_child(args,f_in,f_out);
129 out_of_memory("do_cmd");
130 return 0; /* not reached */
136 static char *get_local_name(struct file_list *flist,char *name)
139 extern int orig_umask;
141 if (do_stat(name,&st) == 0) {
142 if (S_ISDIR(st.st_mode)) {
143 if (chdir(name) != 0) {
144 rprintf(FERROR,"chdir %s : %s (1)\n",name,strerror(errno));
149 if (flist->count > 1) {
150 rprintf(FERROR,"ERROR: destination must be a directory when copying more than 1 file\n");
156 if (flist->count == 1)
162 if (do_mkdir(name,0777 & ~orig_umask) != 0) {
163 rprintf(FERROR,"mkdir %s : %s (1)\n",name,strerror(errno));
166 rprintf(FINFO,"created directory %s\n",name);
169 if (chdir(name) != 0) {
170 rprintf(FERROR,"chdir %s : %s (2)\n",name,strerror(errno));
180 static void do_server_sender(int f_in, int f_out, int argc,char *argv[])
183 struct file_list *flist;
185 extern int relative_paths;
186 extern int am_daemon;
190 rprintf(FINFO,"server_sender starting pid=%d\n",(int)getpid());
192 if (!relative_paths && chdir(dir) != 0) {
193 rprintf(FERROR,"chdir %s: %s (3)\n",dir,strerror(errno));
199 if (strcmp(dir,".")) {
201 if (strcmp(dir,"/") == 0)
208 extern int module_id;
209 char *name = lp_name(module_id);
210 int l = strlen(name);
211 for (i=0;i<argc;i++) {
212 if (strncmp(argv[i], name, l) == 0) {
214 if (!*argv[i]) argv[i] = ".";
219 if (argc == 0 && recurse) {
225 flist = send_file_list(f_out,argc,argv);
226 send_files(flist,f_out,f_in);
232 static int do_recv(int f_in,int f_out,struct file_list *flist,char *local_name)
237 extern int preserve_hard_links;
239 if (preserve_hard_links)
240 init_hard_links(flist);
242 if (pipe(recv_pipe) < 0) {
243 rprintf(FERROR,"pipe failed in do_recv\n");
248 if ((pid=do_fork()) == 0) {
249 recv_files(f_in,flist,local_name,recv_pipe[1]);
251 rprintf(FINFO,"receiver read %ld\n",(long)read_total());
255 generate_files(f_out,flist,local_name,recv_pipe[0]);
257 waitpid(pid, &status, 0);
263 static void do_server_recv(int f_in, int f_out, int argc,char *argv[])
266 struct file_list *flist;
267 char *local_name=NULL;
269 extern int delete_mode;
270 extern int am_daemon;
273 rprintf(FINFO,"server_recv(%d) starting pid=%d\n",argc,(int)getpid());
276 extern int module_id;
277 char *name = lp_name(module_id);
278 int i, l = strlen(name);
279 for (i=0;i<argc;i++) {
280 if (strncmp(argv[i], name, l) == 0) {
282 if (!*argv[i]) argv[i] = ".";
291 if (!am_daemon && chdir(dir) != 0) {
292 rprintf(FERROR,"chdir %s : %s (4)\n",
293 dir,strerror(errno));
299 recv_exclude_list(f_in);
301 flist = recv_file_list(f_in);
302 if (!flist || flist->count == 0) {
303 rprintf(FERROR,"nothing to do\n");
308 if (strcmp(dir,".")) {
309 argv[0] += strlen(dir);
310 if (argv[0][0] == '/') argv[0]++;
312 local_name = get_local_name(flist,argv[0]);
315 status = do_recv(f_in,f_out,flist,local_name);
316 exit_cleanup(status);
320 void start_server(int f_in, int f_out, int argc, char *argv[])
322 extern int cvs_exclude;
323 extern int am_sender;
325 setup_protocol(f_out, f_in);
328 recv_exclude_list(f_in);
331 do_server_sender(f_in, f_out, argc, argv);
333 do_server_recv(f_in, f_out, argc, argv);
338 int client_run(int f_in, int f_out, int pid, int argc, char *argv[])
340 struct file_list *flist;
341 int status = 0, status2 = 0;
342 char *local_name = NULL;
343 extern int am_sender;
345 setup_protocol(f_out,f_in);
348 extern int cvs_exclude;
349 extern int delete_mode;
353 send_exclude_list(f_out);
354 flist = send_file_list(f_out,argc,argv);
356 rprintf(FINFO,"file list sent\n");
357 send_files(flist,f_out,f_in);
360 rprintf(FINFO,"waiting on %d\n",pid);
361 waitpid(pid, &status, 0);
364 exit_cleanup(status);
367 send_exclude_list(f_out);
369 flist = recv_file_list(f_in);
370 if (!flist || flist->count == 0) {
371 rprintf(FINFO,"nothing to do\n");
375 local_name = get_local_name(flist,argv[0]);
377 status2 = do_recv(f_in,f_out,flist,local_name);
382 waitpid(pid, &status, 0);
385 return status | status2;
389 int start_client(int argc, char *argv[])
392 char *shell_machine = NULL;
393 char *shell_path = NULL;
394 char *shell_user = NULL;
397 extern int local_server;
398 extern int am_sender;
399 extern char *shell_cmd;
401 p = strchr(argv[0],':');
406 return start_socket_client(argv[0], p+2, argc-1, argv+1);
416 shell_machine = argv[0];
423 p = strchr(argv[argc-1],':');
426 } else if (p[1] == ':') {
428 return start_socket_client(argv[argc-1], p+2, argc-1, argv);
437 shell_machine = NULL;
438 shell_path = argv[argc-1];
441 shell_machine = argv[argc-1];
448 p = strchr(shell_machine,'@');
451 shell_user = shell_machine;
457 rprintf(FINFO,"cmd=%s machine=%s user=%s path=%s\n",
458 shell_cmd?shell_cmd:"",
459 shell_machine?shell_machine:"",
460 shell_user?shell_user:"",
461 shell_path?shell_path:"");
464 if (!am_sender && argc != 1) {
469 pid = do_cmd(shell_cmd,shell_machine,shell_user,shell_path,&f_in,&f_out);
476 return client_run(f_in, f_out, pid, argc, argv);
480 RETSIGTYPE sigusr1_handler(int val) {
484 int main(int argc,char *argv[])
487 extern int orig_umask;
489 extern int am_daemon;
490 extern int am_server;
492 signal(SIGUSR1, sigusr1_handler);
494 starttime = time(NULL);
495 am_root = (getuid() == 0);
502 /* we set a 0 umask so that correct file permissions can be
504 orig_umask = (int)umask(0);
506 parse_arguments(argc, argv);
512 signal(SIGCHLD,SIG_IGN);
513 signal(SIGINT,SIGNAL_CAST sig_int);
514 signal(SIGPIPE,SIGNAL_CAST sig_int);
515 signal(SIGHUP,SIGNAL_CAST sig_int);
518 return daemon_main();
522 verbose = MAX(verbose,1);
524 #ifndef SUPPORT_LINKS
525 if (!am_server && preserve_links) {
526 rprintf(FERROR,"ERROR: symbolic links not supported\n");
532 start_server(STDIN_FILENO, STDOUT_FILENO, argc, argv);
535 return start_client(argc, argv);