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