Add "use chroot" and "pid file" rsyncd.conf options. The former allows one
[rsync/rsync.git] / main.c
diff --git a/main.c b/main.c
index fa4fe0f..94e358f 100644 (file)
--- a/main.c
+++ b/main.c
@@ -20,7 +20,8 @@
 #include "rsync.h"
 
 time_t starttime = 0;
-int64 total_size = 0;
+
+struct stats stats;
 
 extern int csum_length;
 
@@ -28,16 +29,17 @@ extern int verbose;
 
 static void report(int f)
 {
-       int64 in,out,tsize;
        time_t t = time(NULL);
        extern int am_server;
        extern int am_sender;
        extern int am_daemon;
+       extern int do_stats;
 
        if (am_daemon) {
                syslog(LOG_INFO,"wrote %.0f bytes  read %.0f bytes  total size %.0f\n",
-                      (double)write_total(),(double)read_total(),
-                      (double)total_size);
+                      (double)stats.total_written,
+                      (double)stats.total_read,
+                      (double)stats.total_size);
                if (f == -1 || !am_sender) return;
        }
 
@@ -46,26 +48,49 @@ static void report(int f)
        if (am_server && !am_sender) return;
 
        if (am_server && am_sender) {
-               write_longint(f,read_total());
-               write_longint(f,write_total());
-               write_longint(f,total_size);
+               write_longint(f,stats.total_read);
+               write_longint(f,stats.total_written);
+               write_longint(f,stats.total_size);
                return;
        }
     
-       if (am_sender) {
-               in = read_total();
-               out = write_total();
-               tsize = total_size;
-       } else {
-               out = read_longint(f);
-               in = read_longint(f);
-               tsize = read_longint(f);
+       if (!am_sender) {
+               int64 r;
+               stats.total_written = read_longint(f);
+               r = read_longint(f);
+               stats.total_size = read_longint(f);
+               stats.total_read = r;
+       }
+
+       if (do_stats) {
+               rprintf(FINFO,"\nNumber of files: %d\n", stats.num_files);
+               rprintf(FINFO,"Number of files transferred: %d\n", 
+                      stats.num_transferred_files);
+               rprintf(FINFO,"Total file size: %.0f bytes\n", 
+                      (double)stats.total_size);
+               rprintf(FINFO,"Total transferred file size: %.0f bytes\n", 
+                      (double)stats.total_transferred_size);
+               rprintf(FINFO,"Literal data: %.0f bytes\n", 
+                      (double)stats.literal_data);
+               rprintf(FINFO,"Matched data: %.0f bytes\n", 
+                      (double)stats.matched_data);
+               rprintf(FINFO,"File list size: %d\n", stats.flist_size);
+               rprintf(FINFO,"Total bytes written: %.0f\n", 
+                      (double)stats.total_written);
+               rprintf(FINFO,"Total bytes read: %.0f\n\n", 
+                      (double)stats.total_read);
        }
        
-       printf("wrote %.0f bytes  read %.0f bytes  %.2f bytes/sec\n",
-              (double)out,(double)in,(in+out)/(0.5 + (t-starttime)));
-       printf("total size is %.0f  speedup is %.2f\n",
-              (double)tsize,(1.0*tsize)/(in+out));
+       rprintf(FINFO,"wrote %.0f bytes  read %.0f bytes  %.2f bytes/sec\n",
+              (double)stats.total_written,
+              (double)stats.total_read,
+              (stats.total_written+stats.total_read)/(0.5 + (t-starttime)));
+       rprintf(FINFO,"total size is %.0f  speedup is %.2f\n",
+              (double)stats.total_size,
+              (1.0*stats.total_size)/(stats.total_written+stats.total_read));
+
+       fflush(stdout);
+       fflush(stderr);
 }
 
 
@@ -147,6 +172,10 @@ static char *get_local_name(struct file_list *flist,char *name)
        STRUCT_STAT st;
        extern int orig_umask;
 
+       if (verbose > 2)
+               rprintf(FINFO,"get_local_name count=%d %s\n", 
+                       flist->count, name);
+
        if (do_stat(name,&st) == 0) {
                if (S_ISDIR(st.st_mode)) {
                        if (!push_dir(name, 0)) {
@@ -173,7 +202,8 @@ static char *get_local_name(struct file_list *flist,char *name)
                rprintf(FERROR,"mkdir %s : %s (1)\n",name,strerror(errno));
                exit_cleanup(1);
        } else {
-               rprintf(FINFO,"created directory %s\n",name);
+               if (verbose > 0)
+                       rprintf(FINFO,"created directory %s\n",name);
        }
 
        if (!push_dir(name, 0)) {
@@ -220,6 +250,10 @@ static void do_server_sender(int f_in, int f_out, int argc,char *argv[])
                argv[0] = ".";
        }
        
+       set_nonblocking(f_out);
+       if (f_in != f_out)
+               set_nonblocking(f_in);
+               
        flist = send_file_list(f_out,argc,argv);
        if (!flist || flist->count == 0) {
                exit_cleanup(0);
@@ -253,12 +287,12 @@ static int do_recv(int f_in,int f_out,struct file_list *flist,char *local_name)
                close(recv_pipe[0]);
                if (f_in != f_out) close(f_out);
 
+               set_nonblocking(f_in);
+               set_nonblocking(recv_pipe[1]);
+
                recv_files(f_in,flist,local_name,recv_pipe[1]);
                report(f_in);
 
-               if (verbose > 3)
-                       rprintf(FINFO,"do_recv waiting on %d\n",pid);
-
                io_flush();
                _exit(0);
        }
@@ -266,6 +300,12 @@ static int do_recv(int f_in,int f_out,struct file_list *flist,char *local_name)
        close(recv_pipe[1]);
        io_close_input(f_in);
        if (f_in != f_out) close(f_in);
+
+       set_nonblocking(f_out);
+       set_nonblocking(recv_pipe[0]);
+
+       io_start_buffering(f_out);
+
        generate_files(f_out,flist,local_name,recv_pipe[0]);
 
        io_flush();
@@ -324,8 +364,12 @@ void start_server(int f_in, int f_out, int argc, char *argv[])
        extern int cvs_exclude;
        extern int am_sender;
 
+       set_nonblocking(f_out);
+       if (f_in != f_out)
+               set_nonblocking(f_in);
+                       
        setup_protocol(f_out, f_in);
-       
+
        if (am_sender) {
                recv_exclude_list(f_in);
                if (cvs_exclude)
@@ -356,6 +400,11 @@ int client_run(int f_in, int f_out, int pid, int argc, char *argv[])
                flist = send_file_list(f_out,argc,argv);
                if (verbose > 3) 
                        rprintf(FINFO,"file list sent\n");
+
+               set_nonblocking(f_out);
+               if (f_in != f_out)
+                       set_nonblocking(f_in);
+
                send_files(flist,f_out,f_in);
                if (pid != -1) {
                        if (verbose > 3)
@@ -389,20 +438,34 @@ int client_run(int f_in, int f_out, int pid, int argc, char *argv[])
        return status | status2;
 }
 
+static char *find_colon(char *s)
+{
+       char *p, *p2;
+
+       p = strchr(s,':');
+       if (!p) return NULL;
+       
+       /* now check to see if there is a / in the string before the : - if there is then
+          discard the colon on the assumption that the : is part of a filename */
+       p2 = strchr(s,'/');
+       if (p2 && p2 < p) return NULL;
+
+       return p;
+}
 
-int start_client(int argc, char *argv[])
+static int start_client(int argc, char *argv[])
 {
        char *p;
        char *shell_machine = NULL;
        char *shell_path = NULL;
        char *shell_user = NULL;
-       int pid;
+       int pid, ret;
        int f_in,f_out;
        extern int local_server;
        extern int am_sender;
        extern char *shell_cmd;
 
-       p = strchr(argv[0],':');
+       p = find_colon(argv[0]);
 
        if (p) {
                if (p[1] == ':') {
@@ -424,7 +487,7 @@ int start_client(int argc, char *argv[])
        } else {
                am_sender = 1;
 
-               p = strchr(argv[argc-1],':');
+               p = find_colon(argv[argc-1]);
                if (!p) {
                        local_server = 1;
                } else if (p[1] == ':') {
@@ -472,16 +535,16 @@ int start_client(int argc, char *argv[])
        
        pid = do_cmd(shell_cmd,shell_machine,shell_user,shell_path,&f_in,&f_out);
        
-#if HAVE_SETLINEBUF
-       setlinebuf(stdout);
-       setlinebuf(stderr);
-#endif
+       ret = client_run(f_in, f_out, pid, argc, argv);
 
-       return client_run(f_in, f_out, pid, argc, argv);
+       fflush(stdout);
+       fflush(stderr);
+
+       return ret;
 }
 
 
-RETSIGTYPE sigusr1_handler(int val) {
+static RETSIGTYPE sigusr1_handler(int val) {
        exit_cleanup(1);
 }
 
@@ -498,6 +561,8 @@ int main(int argc,char *argv[])
        starttime = time(NULL);
        am_root = (getuid() == 0);
 
+       memset(&stats, 0, sizeof(stats));
+
        if (argc < 2) {
                usage(FERROR);
                exit_cleanup(1);
@@ -507,7 +572,9 @@ int main(int argc,char *argv[])
           carried across */
        orig_umask = (int)umask(0);
 
-       parse_arguments(argc, argv);
+       if (!parse_arguments(argc, argv)) {
+               exit_cleanup(1);
+       }
 
        argc -= optind;
        argv += optind;
@@ -517,6 +584,7 @@ int main(int argc,char *argv[])
        signal(SIGINT,SIGNAL_CAST sig_int);
        signal(SIGPIPE,SIGNAL_CAST sig_int);
        signal(SIGHUP,SIGNAL_CAST sig_int);
+       signal(SIGTERM,SIGNAL_CAST sig_int);
 
        if (am_daemon) {
                return daemon_main();