X-Git-Url: https://mattmccutchen.net/rsync/rsync.git/blobdiff_plain/22d6234e04e087b9111243d1f940db7c6d59fd76..654175798bdbdd6403e10c8fa74e8586b3612ea1:/main.c diff --git a/main.c b/main.c index 5530e7c3..bc587ccc 100644 --- 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,43 +29,65 @@ 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) { - rprintf(FINFO, "wrote %.0f bytes read %.0f bytes total size %.0f\n", - (double)write_total(),(double)read_total(), - (double)total_size); + log_exit(0); if (f == -1 || !am_sender) return; } if (!verbose) return; + 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_flush(f); + 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); } @@ -146,40 +169,47 @@ static char *get_local_name(struct file_list *flist,char *name) STRUCT_STAT st; extern int orig_umask; - if (do_stat(name,&st) == 0) { - if (S_ISDIR(st.st_mode)) { - if (chdir(name) != 0) { - rprintf(FERROR,"chdir %s : %s (1)\n",name,strerror(errno)); - exit_cleanup(1); - } - return NULL; - } - if (flist->count > 1) { - rprintf(FERROR,"ERROR: destination must be a directory when copying more than 1 file\n"); - exit_cleanup(1); - } - return name; - } - - if (flist->count == 1) - return name; - - if (!name) - return NULL; - - if (do_mkdir(name,0777 & ~orig_umask) != 0) { - rprintf(FERROR,"mkdir %s : %s (1)\n",name,strerror(errno)); - exit_cleanup(1); - } else { - rprintf(FINFO,"created directory %s\n",name); - } - - if (chdir(name) != 0) { - rprintf(FERROR,"chdir %s : %s (2)\n",name,strerror(errno)); - exit_cleanup(1); - } - - return NULL; + 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)) { + rprintf(FERROR,"push_dir %s : %s (1)\n", + name,strerror(errno)); + exit_cleanup(RERR_FILESELECT); + } + return NULL; + } + if (flist->count > 1) { + rprintf(FERROR,"ERROR: destination must be a directory when copying more than 1 file\n"); + exit_cleanup(RERR_FILESELECT); + } + return name; + } + + if (flist->count == 1) + return name; + + if (!name) + return NULL; + + if (do_mkdir(name,0777 & ~orig_umask) != 0) { + rprintf(FERROR,"mkdir %s : %s (1)\n",name,strerror(errno)); + exit_cleanup(RERR_FILEIO); + } else { + if (verbose > 0) + rprintf(FINFO,"created directory %s\n",name); + } + + if (!push_dir(name, 0)) { + rprintf(FERROR,"push_dir %s : %s (2)\n", + name,strerror(errno)); + exit_cleanup(RERR_FILESELECT); + } + + return NULL; } @@ -196,9 +226,9 @@ static void do_server_sender(int f_in, int f_out, int argc,char *argv[]) if (verbose > 2) rprintf(FINFO,"server_sender starting pid=%d\n",(int)getpid()); - if (!relative_paths && chdir(dir) != 0) { - rprintf(FERROR,"chdir %s: %s (3)\n",dir,strerror(errno)); - exit_cleanup(1); + if (!relative_paths && !push_dir(dir, 0)) { + rprintf(FERROR,"push_dir %s: %s (3)\n",dir,strerror(errno)); + exit_cleanup(RERR_FILESELECT); } argc--; argv++; @@ -217,9 +247,18 @@ 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); + } + send_files(flist,f_out,f_in); report(f_out); + io_flush(); exit_cleanup(0); } @@ -230,31 +269,44 @@ static int do_recv(int f_in,int f_out,struct file_list *flist,char *local_name) int status=0; int recv_pipe[2]; extern int preserve_hard_links; - extern int am_daemon; if (preserve_hard_links) init_hard_links(flist); if (pipe(recv_pipe) < 0) { rprintf(FERROR,"pipe failed in do_recv\n"); - exit(1); + exit_cleanup(RERR_SOCKETIO); } + io_flush(); if ((pid=do_fork()) == 0) { close(recv_pipe[0]); - close(f_out); + 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]); - if (am_daemon) report(-1); - exit_cleanup(0); + report(f_in); + + io_flush(); + _exit(0); } close(recv_pipe[1]); - close(f_in); + 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(); waitpid(pid, &status, 0); - return status; } @@ -275,10 +327,10 @@ static void do_server_recv(int f_in, int f_out, int argc,char *argv[]) dir = argv[0]; argc--; argv++; - if (!am_daemon && chdir(dir) != 0) { - rprintf(FERROR,"chdir %s : %s (4)\n", + if (!am_daemon && !push_dir(dir, 0)) { + rprintf(FERROR,"push_dir %s : %s (4)\n", dir,strerror(errno)); - exit_cleanup(1); + exit_cleanup(RERR_FILESELECT); } } @@ -287,8 +339,8 @@ static void do_server_recv(int f_in, int f_out, int argc,char *argv[]) flist = recv_file_list(f_in); if (!flist || flist->count == 0) { - rprintf(FERROR,"nothing to do\n"); - exit_cleanup(1); + rprintf(FERROR,"server_recv: nothing to do\n"); + exit_cleanup(RERR_FILESELECT); } if (argc > 0) { @@ -309,8 +361,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) @@ -328,6 +384,7 @@ int client_run(int f_in, int f_out, int pid, int argc, char *argv[]) int status = 0, status2 = 0; char *local_name = NULL; extern int am_sender; + extern int list_only; setup_protocol(f_out,f_in); @@ -341,21 +398,29 @@ 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) - rprintf(FINFO,"waiting on %d\n",pid); + rprintf(FINFO,"client_run waiting on %d\n",pid); + io_flush(); waitpid(pid, &status, 0); } report(-1); exit_cleanup(status); } + + if (argc == 0) list_only = 1; send_exclude_list(f_out); flist = recv_file_list(f_in); if (!flist || flist->count == 0) { - rprintf(FINFO,"nothing to do\n"); + rprintf(FINFO,"client: nothing to do\n"); exit_cleanup(0); } @@ -363,29 +428,58 @@ int client_run(int f_in, int f_out, int pid, int argc, char *argv[]) status2 = do_recv(f_in,f_out,flist,local_name); - report(f_in); - if (pid != -1) { + if (verbose > 3) + rprintf(FINFO,"client_run2 waiting on %d\n",pid); + io_flush(); waitpid(pid, &status, 0); } 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],':'); + if (strncasecmp(URL_PREFIX, argv[0], strlen(URL_PREFIX)) == 0) { + char *host, *path; + + host = argv[0] + strlen(URL_PREFIX); + p = strchr(host,'/'); + if (p) { + *p = 0; + path = p+1; + } else { + path=""; + } + return start_socket_client(host, path, argc-1, argv+1); + } + + p = find_colon(argv[0]); if (p) { if (p[1] == ':') { @@ -393,9 +487,9 @@ int start_client(int argc, char *argv[]) return start_socket_client(argv[0], p+2, argc-1, argv+1); } - if (argc < 2) { + if (argc < 1) { usage(FERROR); - exit_cleanup(1); + exit_cleanup(RERR_SYNTAX); } am_sender = 0; @@ -407,7 +501,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] == ':') { @@ -417,7 +511,7 @@ int start_client(int argc, char *argv[]) if (argc < 2) { usage(FERROR); - exit_cleanup(1); + exit_cleanup(RERR_SYNTAX); } if (local_server) { @@ -448,24 +542,24 @@ int start_client(int argc, char *argv[]) shell_path?shell_path:""); } - if (!am_sender && argc != 1) { + if (!am_sender && argc > 1) { usage(FERROR); - exit_cleanup(1); + exit_cleanup(RERR_SYNTAX); } 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); + + fflush(stdout); + fflush(stderr); - return client_run(f_in, f_out, pid, argc, argv); + return ret; } -RETSIGTYPE sigusr1_handler(int val) { - exit_cleanup(1); +static RETSIGTYPE sigusr1_handler(int val) { + exit_cleanup(RERR_SIGNAL); } int main(int argc,char *argv[]) @@ -481,16 +575,20 @@ int main(int argc,char *argv[]) starttime = time(NULL); am_root = (getuid() == 0); + memset(&stats, 0, sizeof(stats)); + if (argc < 2) { usage(FERROR); - exit(1); + exit_cleanup(RERR_SYNTAX); } /* we set a 0 umask so that correct file permissions can be carried across */ orig_umask = (int)umask(0); - parse_arguments(argc, argv); + if (!parse_arguments(argc, argv)) { + exit_cleanup(RERR_SYNTAX); + } argc -= optind; argv += optind; @@ -500,6 +598,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(); @@ -507,7 +606,7 @@ int main(int argc,char *argv[]) if (argc < 1) { usage(FERROR); - exit(1); + exit_cleanup(RERR_SYNTAX); } if (dry_run) @@ -516,7 +615,7 @@ int main(int argc,char *argv[]) #ifndef SUPPORT_LINKS if (!am_server && preserve_links) { rprintf(FERROR,"ERROR: symbolic links not supported\n"); - exit_cleanup(1); + exit_cleanup(RERR_UNSUPPORTED); } #endif