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