Ignore SIGPIPE and allow EPIPE to get through to the program so that
[rsync/rsync.git] / main.c
diff --git a/main.c b/main.c
index fd30211..a54d67f 100644 (file)
--- a/main.c
+++ b/main.c
@@ -60,6 +60,7 @@ static void report(int f)
        if (do_stats) {
                /* These come out from every process */
                show_malloc_stats();
+               show_flist_stats();
        }
 
        if (am_daemon) {
@@ -176,7 +177,7 @@ static pid_t do_cmd(char *cmd,char *machine,char *user,char *path,int *f_in,int
        extern int blocking_io;
        extern int read_batch;
 
-       if (!read_batch && !local_server) { /* dw -- added read_batch */
+       if (!read_batch && !local_server) {
                if (!cmd)
                        cmd = getenv(RSYNC_RSH_ENV);
                if (!cmd)
@@ -206,10 +207,11 @@ static pid_t do_cmd(char *cmd,char *machine,char *user,char *path,int *f_in,int
 
                args[argc++] = rsync_path;
 
-               server_options(args,&argc);
+               if ((blocking_io == -1) && (strcmp(cmd, RSYNC_RSH) == 0))
+                       blocking_io = 1;
 
+               server_options(args,&argc);
 
-               if (strcmp(cmd, RSYNC_RSH) == 0) blocking_io = 1;
        }
 
        args[argc++] = ".";
@@ -228,7 +230,7 @@ static pid_t do_cmd(char *cmd,char *machine,char *user,char *path,int *f_in,int
 
        if (local_server) {
                if (read_batch)
-                   create_flist_from_batch();
+                   create_flist_from_batch(); /* sets batch_flist */
                ret = local_child(argc, args, f_in, f_out);
        } else {
                ret = piped_child(args,f_in,f_out);
@@ -441,8 +443,8 @@ static void do_server_recv(int f_in, int f_out, int argc,char *argv[])
        extern int am_daemon;
        extern int module_id;
        extern int am_sender;
-       extern int read_batch;   /* dw */
-       extern struct file_list *batch_flist;  /* dw */
+       extern int read_batch;
+       extern struct file_list *batch_flist;
 
        if (verbose > 2)
                rprintf(FINFO,"server_recv(%d) starting pid=%d\n",argc,(int)getpid());
@@ -468,7 +470,7 @@ static void do_server_recv(int f_in, int f_out, int argc,char *argv[])
        if (delete_mode && !delete_excluded)
                recv_exclude_list(f_in);
 
-       if (read_batch) /*  dw  */
+       if (read_batch)
            flist = batch_flist;
        else
            flist = recv_file_list(f_in);
@@ -495,7 +497,7 @@ void start_server(int f_in, int f_out, int argc, char *argv[])
        extern int cvs_exclude;
        extern int am_sender;
        extern int remote_version;
-       extern int read_batch; /* dw */
+       extern int read_batch;
 
        setup_protocol(f_out, f_in);
 
@@ -506,7 +508,7 @@ void start_server(int f_in, int f_out, int argc, char *argv[])
                io_start_multiplex_out(f_out);
 
        if (am_sender) {
-               if (!read_batch) { /* dw */
+               if (!read_batch) {
                    recv_exclude_list(f_in);
                    if (cvs_exclude)
                        add_cvs_excludes();
@@ -525,19 +527,19 @@ void start_server(int f_in, int f_out, int argc, char *argv[])
  */
 int client_run(int f_in, int f_out, pid_t pid, int argc, char *argv[])
 {
-       struct file_list *flist;
+       struct file_list *flist = NULL;
        int status = 0, status2 = 0;
        char *local_name = NULL;
        extern int am_sender;
        extern int remote_version;
        extern pid_t cleanup_child_pid;
-       extern int write_batch; /* dw */
-       extern int read_batch; /* dw */
-       extern struct file_list *batch_flist; /*  dw */
+       extern int write_batch;
+       extern int read_batch;
+       extern struct file_list *batch_flist;
 
        cleanup_child_pid = pid;
        if (read_batch)
-           flist = batch_flist;  /* dw */
+           flist = batch_flist;
 
        set_nonblocking(f_in);
        set_nonblocking(f_out);
@@ -580,7 +582,7 @@ int client_run(int f_in, int f_out, pid_t pid, int argc, char *argv[])
                list_only = 1;
        }
        
-       if (!write_batch) /* dw */
+       if (!write_batch)
            send_exclude_list(f_out);
        
        flist = recv_file_list(f_in);
@@ -656,6 +658,7 @@ static int start_client(int argc, char *argv[])
        extern char *shell_cmd;
        extern int rsync_port;
        extern int whole_file;
+       extern int write_batch;
        extern int read_batch;
        int rc;
 
@@ -683,7 +686,7 @@ static int start_client(int argc, char *argv[])
                return start_socket_client(host, path, argc-1, argv+1);
        }
 
-       if (!read_batch) { /* dw */
+       if (!read_batch) {
            p = find_colon(argv[0]);
 
        if (p) {
@@ -709,8 +712,12 @@ static int start_client(int argc, char *argv[])
                p = find_colon(argv[argc-1]);
                if (!p) {
                        local_server = 1;
-                       /* disable "rsync algorithm" when both sides local */
-                       whole_file = 1;
+                       /*
+                        * disable "rsync algorithm" when both sides local,
+                        * except when creating a batch update
+                        */
+                       if (!write_batch && whole_file == -1)
+                               whole_file = 1;
                } else if (p[1] == ':') {
                        *p = 0;
                        return start_socket_client(argv[argc-1], p+2, argc-1, argv);
@@ -732,9 +739,9 @@ static int start_client(int argc, char *argv[])
                argc--;
        }
        } else {
-           am_sender = 1;  /*  dw */
-           local_server = 1;  /* dw */
-           shell_path = argv[argc-1];  /* dw */
+           am_sender = 1;
+           local_server = 1;
+           shell_path = argv[argc-1];
        }
 
        if (shell_machine) {
@@ -799,13 +806,11 @@ int main(int argc,char *argv[])
        extern int am_daemon;
        extern int am_server;
        int ret;
-       extern int read_batch;   /*  dw */
-       extern int write_batch;  /*  dw */
-       extern char *batch_ext;   /*  dw */
-       int orig_argc;  /* dw */
+       extern int write_batch;
+       int orig_argc;
        char **orig_argv;
 
-       orig_argc = argc;   /* dw */
+       orig_argc = argc;
        orig_argv = argv;
 
        signal(SIGUSR1, sigusr1_handler);
@@ -834,25 +839,23 @@ 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);
 
+       /* Ignore SIGPIPE; we consistently check error codes and will
+        * see the EPIPE. */
+       signal(SIGPIPE, SIG_IGN);
+
        /* Initialize push_dir here because on some old systems getcwd
           (implemented by forking "pwd" and reading its output) doesn't
           work when there are other child processes.  Also, on all systems
           that implement getcwd that way "pwd" can't be found after chroot. */
        push_dir(NULL,0);
 
-       if (write_batch) { /* dw */
-           create_batch_file_ext();
+       if (write_batch && !am_server) {
            write_batch_argvs_file(orig_argc, orig_argv);
        }
 
-       if (read_batch) { /* dw */
-           set_batch_file_ext(batch_ext);
-       }
-
        if (am_daemon) {
                return daemon_main();
        }
@@ -879,7 +882,9 @@ int main(int argc,char *argv[])
        }
 
        ret = start_client(argc, argv);
-       exit_cleanup(ret);
+       if (ret == -1) 
+           exit_cleanup(RERR_STARTCLIENT);
+       else
+           exit_cleanup(ret);
        return ret;
 }
-