From 9486289ce452ab82ca8b3f16fae8c2ef5247a923 Mon Sep 17 00:00:00 2001 From: Andrew Tridgell Date: Thu, 7 May 1998 14:50:22 +0000 Subject: [PATCH] some large systematic changes to make socket conversion easier. The biggest one is the use of rprintf() instead of fprintf() --- Makefile.in | 2 +- compat.c | 4 +- configure.in | 2 +- exclude.c | 7 +- flist.c | 48 ++++----- hlink.c | 7 +- io.c | 20 ++-- main.c | 296 +++++++++++++++++++++++++++++---------------------- match.c | 22 ++-- md4.c | 4 +- rsync.c | 156 +++++++++++++-------------- rsync.h | 8 +- token.c | 14 +-- util.c | 92 ++++++++++++---- 14 files changed, 393 insertions(+), 289 deletions(-) diff --git a/Makefile.in b/Makefile.in index d0184870..8d1c173f 100644 --- a/Makefile.in +++ b/Makefile.in @@ -22,7 +22,7 @@ SHELL=/bin/sh LIBOBJ=lib/getopt.o lib/fnmatch.o lib/zlib.o lib/compat.o OBJS1=rsync.o exclude.o util.o md4.o main.o checksum.o match.o syscall.o -OBJS=$(OBJS1) flist.o io.o compat.o hlink.o token.o uidlist.o $(LIBOBJ) +OBJS=$(OBJS1) flist.o io.o compat.o hlink.o token.o uidlist.o socket.o $(LIBOBJ) # note that the -I. is needed to handle config.h when using VPATH .c.o: diff --git a/compat.c b/compat.c index ea9b67df..84010215 100644 --- a/compat.c +++ b/compat.c @@ -52,12 +52,12 @@ void setup_protocol(int f_out,int f_in) if (remote_version < MIN_PROTOCOL_VERSION || remote_version > MAX_PROTOCOL_VERSION) { - fprintf(FERROR,"protocol version mismatch - is your shell clean?\n"); + rprintf(FERROR,"protocol version mismatch - is your shell clean?\n"); exit_cleanup(1); } if (verbose > 2) - fprintf(FINFO, "local_version=%d remote_version=%d\n", + rprintf(FINFO, "local_version=%d remote_version=%d\n", PROTOCOL_VERSION, remote_version); if (remote_version >= 12) { diff --git a/configure.in b/configure.in index 4424255d..9749558d 100644 --- a/configure.in +++ b/configure.in @@ -42,7 +42,7 @@ AC_FUNC_MMAP AC_FUNC_UTIME_NULL AC_CHECK_FUNCS(waitpid strtok pipe getcwd mkdir strdup strerror chown chmod mknod) AC_CHECK_FUNCS(fchmod fstat strchr bcopy bzero readlink link utime utimes) -AC_CHECK_FUNCS(memmove getopt_long lchown setlinebuf) +AC_CHECK_FUNCS(memmove getopt_long lchown setlinebuf vsnprintf) echo $ac_n "checking for working fnmatch... $ac_c" AC_TRY_RUN([#include diff --git a/exclude.c b/exclude.c index 7504f968..b2831e4a 100644 --- a/exclude.c +++ b/exclude.c @@ -24,7 +24,6 @@ #include "rsync.h" extern int verbose; -extern int am_server; static char **exclude_list; @@ -89,7 +88,7 @@ void add_exclude_list(char *pattern,char ***list) if (strcmp(pattern,"!") == 0) { if (verbose > 2) - fprintf(FINFO,"clearing exclude list\n"); + rprintf(FINFO,"clearing exclude list\n"); while ((len)--) free((*list)[len]); free((*list)); @@ -107,7 +106,7 @@ void add_exclude_list(char *pattern,char ***list) out_of_memory("add_exclude"); if (verbose > 2) - fprintf(FINFO,"add_exclude(%s)\n",pattern); + rprintf(FINFO,"add_exclude(%s)\n",pattern); (*list)[len+1] = NULL; } @@ -124,7 +123,7 @@ char **make_exclude_list(char *fname,char **list1,int fatal) char line[MAXPATHLEN]; if (!f) { if (fatal) { - fprintf(FERROR,"%s : %s\n",fname,strerror(errno)); + rprintf(FERROR,"%s : %s\n",fname,strerror(errno)); exit_cleanup(1); } return list; diff --git a/flist.c b/flist.c index 53038739..494b1088 100644 --- a/flist.c +++ b/flist.c @@ -69,7 +69,7 @@ static int match_file_name(char *fname,STRUCT_STAT *st) { if (check_exclude(fname,local_exclude_list)) { if (verbose > 2) - fprintf(FINFO,"excluding file %s\n",fname); + rprintf(FINFO,"excluding file %s\n",fname); return 0; } return 1; @@ -360,13 +360,13 @@ static struct file_struct *make_file(char *fname) if (link_stat(fname,&st) != 0) { io_error = 1; - fprintf(FERROR,"%s: %s\n", + rprintf(FERROR,"%s: %s\n", fname,strerror(errno)); return NULL; } if (S_ISDIR(st.st_mode) && !recurse) { - fprintf(FINFO,"skipping directory %s\n",fname); + rprintf(FINFO,"skipping directory %s\n",fname); return NULL; } @@ -379,7 +379,7 @@ static struct file_struct *make_file(char *fname) return NULL; if (verbose > 2) - fprintf(FINFO,"make_file(%s)\n",fname); + rprintf(FINFO,"make_file(%s)\n",fname); file = (struct file_struct *)malloc(sizeof(*file)); if (!file) out_of_memory("make_file"); @@ -418,7 +418,7 @@ static struct file_struct *make_file(char *fname) char lnk[MAXPATHLEN]; if ((l=readlink(fname,lnk,MAXPATHLEN-1)) == -1) { io_error=1; - fprintf(FERROR,"readlink %s : %s\n", + rprintf(FERROR,"readlink %s : %s\n", fname,strerror(errno)); return NULL; } @@ -507,7 +507,7 @@ static void send_directory(int f,struct file_list *flist,char *dir) d = opendir(dir); if (!d) { io_error = 1; - fprintf(FERROR,"%s: %s\n", + rprintf(FERROR,"%s: %s\n", dir,strerror(errno)); return; } @@ -518,7 +518,7 @@ static void send_directory(int f,struct file_list *flist,char *dir) if (fname[l-1] != '/') { if (l == MAXPATHLEN-1) { io_error = 1; - fprintf(FERROR,"skipping long-named directory %s\n",fname); + rprintf(FERROR,"skipping long-named directory %s\n",fname); closedir(d); return; } @@ -533,7 +533,7 @@ static void send_directory(int f,struct file_list *flist,char *dir) local_exclude_list = make_exclude_list(fname,NULL,0); } else { io_error = 1; - fprintf(FINFO,"cannot cvs-exclude in long-named directory %s\n",fname); + rprintf(FINFO,"cannot cvs-exclude in long-named directory %s\n",fname); } } @@ -561,8 +561,8 @@ struct file_list *send_file_list(int f,int argc,char *argv[]) struct file_list *flist; if (verbose && recurse && !am_server && f != -1) { - fprintf(FINFO,"building file list ... "); - fflush(FINFO); + rprintf(FINFO,"building file list ... "); + rflush(FINFO); } flist = (struct file_list *)malloc(sizeof(flist[0])); @@ -588,12 +588,12 @@ struct file_list *send_file_list(int f,int argc,char *argv[]) if (link_stat(fname,&st) != 0) { io_error=1; - fprintf(FERROR,"%s : %s\n",fname,strerror(errno)); + rprintf(FERROR,"%s : %s\n",fname,strerror(errno)); continue; } if (S_ISDIR(st.st_mode) && !recurse) { - fprintf(FINFO,"skipping directory %s\n",fname); + rprintf(FINFO,"skipping directory %s\n",fname); continue; } @@ -631,12 +631,12 @@ struct file_list *send_file_list(int f,int argc,char *argv[]) if (dir && *dir) { if (getcwd(dbuf,MAXPATHLEN-1) == NULL) { - fprintf(FERROR,"getwd : %s\n",strerror(errno)); + rprintf(FERROR,"getwd : %s\n",strerror(errno)); exit_cleanup(1); } if (chdir(dir) != 0) { io_error=1; - fprintf(FERROR,"chdir %s : %s\n", + rprintf(FERROR,"chdir %s : %s\n", dir,strerror(errno)); continue; } @@ -646,7 +646,7 @@ struct file_list *send_file_list(int f,int argc,char *argv[]) send_file_name(f,flist,fname,recurse,FLAG_DELETE); flist_dir = NULL; if (chdir(dbuf) != 0) { - fprintf(FERROR,"chdir %s : %s\n", + rprintf(FERROR,"chdir %s : %s\n", dbuf,strerror(errno)); exit_cleanup(1); } @@ -664,7 +664,7 @@ struct file_list *send_file_list(int f,int argc,char *argv[]) } if (verbose && recurse && !am_server && f != -1) - fprintf(FINFO,"done\n"); + rprintf(FINFO,"done\n"); clean_flist(flist); @@ -680,7 +680,7 @@ struct file_list *send_file_list(int f,int argc,char *argv[]) } if (verbose > 2) - fprintf(FINFO,"send_file_list done\n"); + rprintf(FINFO,"send_file_list done\n"); return flist; } @@ -692,8 +692,8 @@ struct file_list *recv_file_list(int f) unsigned char flags; if (verbose && recurse && !am_server) { - fprintf(FINFO,"receiving file list ... "); - fflush(FINFO); + rprintf(FINFO,"receiving file list ... "); + rflush(FINFO); } flist = (struct file_list *)malloc(sizeof(flist[0])); @@ -731,17 +731,17 @@ struct file_list *recv_file_list(int f) flist->count++; if (verbose > 2) - fprintf(FINFO,"recv_file_name(%s)\n",f_name(flist->files[i])); + rprintf(FINFO,"recv_file_name(%s)\n",f_name(flist->files[i])); } if (verbose > 2) - fprintf(FINFO,"received %d names\n",flist->count); + rprintf(FINFO,"received %d names\n",flist->count); clean_flist(flist); if (verbose && recurse && !am_server) { - fprintf(FINFO,"done\n"); + rprintf(FINFO,"done\n"); } /* now recv the uid/gid list. This was introduced in protocol version 15 */ @@ -755,7 +755,7 @@ struct file_list *recv_file_list(int f) } if (verbose > 2) - fprintf(FINFO,"recv_file_list done\n"); + rprintf(FINFO,"recv_file_list done\n"); return flist; @@ -850,7 +850,7 @@ void clean_flist(struct file_list *flist) strcmp(f_name(flist->files[i]), f_name(flist->files[i-1])) == 0) { if (verbose > 1 && !am_server) - fprintf(FINFO,"removing duplicate name %s from file list %d\n", + rprintf(FINFO,"removing duplicate name %s from file list %d\n", f_name(flist->files[i-1]),i-1); free_file(flist->files[i]); } diff --git a/hlink.c b/hlink.c index 6b2c0605..ed6d75ec 100644 --- a/hlink.c +++ b/hlink.c @@ -19,7 +19,6 @@ #include "rsync.h" -extern int am_server; extern int dry_run; extern int verbose; @@ -113,7 +112,7 @@ static void hard_link_one(int i) if (link_stat(f_name(&hlink_list[i]),&st2) != 0) { if (do_link(f_name(&hlink_list[i-1]),f_name(&hlink_list[i])) != 0) { if (verbose > 0) - fprintf(FINFO,"link %s => %s : %s\n", + rprintf(FINFO,"link %s => %s : %s\n", f_name(&hlink_list[i]), f_name(&hlink_list[i-1]),strerror(errno)); return; @@ -124,14 +123,14 @@ static void hard_link_one(int i) if (do_unlink(f_name(&hlink_list[i])) != 0 || do_link(f_name(&hlink_list[i-1]),f_name(&hlink_list[i])) != 0) { if (verbose > 0) - fprintf(FINFO,"link %s => %s : %s\n", + rprintf(FINFO,"link %s => %s : %s\n", f_name(&hlink_list[i]), f_name(&hlink_list[i-1]),strerror(errno)); return; } } if (verbose > 0) - fprintf(FINFO,"%s => %s\n", + rprintf(FINFO,"%s => %s\n", f_name(&hlink_list[i]),f_name(&hlink_list[i-1])); } #endif diff --git a/io.c b/io.c index 1186740a..f5816ed9 100644 --- a/io.c +++ b/io.c @@ -113,7 +113,7 @@ static void check_timeout(void) t = time(NULL); if (last_io && io_timeout && (t-last_io)>io_timeout) { - fprintf(FERROR,"read timeout after %d second - exiting\n", + rprintf(FERROR,"read timeout after %d second - exiting\n", (int)(t-last_io)); exit_cleanup(1); } @@ -170,7 +170,7 @@ int32 read_int(int f) char b[4]; if ((ret=readfd(f,b,4)) != 4) { if (verbose > 1) - fprintf(FERROR,"(%d) Error reading %d bytes : %s\n", + rprintf(FERROR,"(%d) Error reading %d bytes : %s\n", getpid(),4,ret==-1?strerror(errno):"EOF"); exit_cleanup(1); } @@ -188,13 +188,13 @@ int64 read_longint(int f) if ((int32)ret != (int32)0xffffffff) return ret; #ifdef NO_INT64 - fprintf(FERROR,"Integer overflow - attempted 64 bit offset\n"); + rprintf(FERROR,"Integer overflow - attempted 64 bit offset\n"); exit_cleanup(1); #else if (remote_version >= 16) { if ((ret=readfd(f,b,8)) != 8) { if (verbose > 1) - fprintf(FERROR,"(%d) Error reading %d bytes : %s\n", + rprintf(FERROR,"(%d) Error reading %d bytes : %s\n", getpid(),8,ret==-1?strerror(errno):"EOF"); exit_cleanup(1); } @@ -211,7 +211,7 @@ void read_buf(int f,char *buf,int len) int ret; if ((ret=readfd(f,buf,len)) != len) { if (verbose > 1) - fprintf(FERROR,"(%d) Error reading %d bytes : %s\n", + rprintf(FERROR,"(%d) Error reading %d bytes : %s\n", getpid(),len,ret==-1?strerror(errno):"EOF"); exit_cleanup(1); } @@ -324,7 +324,7 @@ static int writefd(int fd,char *buf,int len) systems it seems (eg. IRIX) */ u_sleep(1000); #if 0 - fprintf(FERROR,"write exception\n"); + rprintf(FERROR,"write exception\n"); exit_cleanup(1); #endif } @@ -351,7 +351,7 @@ static int writefd(int fd,char *buf,int len) &w_fds,NULL,&tv); if (count == -1 && errno != EINTR) { if (verbose > 1) - fprintf(FERROR,"select error: %s\n", strerror(errno)); + rprintf(FERROR,"select error: %s\n", strerror(errno)); exit_cleanup(1); } @@ -382,7 +382,7 @@ void write_int(int f,int32 x) char b[4]; SIVAL(b,0,x); if ((ret=writefd(f,b,4)) != 4) { - fprintf(FERROR,"write_int failed : %s\n", + rprintf(FERROR,"write_int failed : %s\n", ret==-1?strerror(errno):"EOF"); exit_cleanup(1); } @@ -405,7 +405,7 @@ void write_longint(int f, int64 x) SIVAL(b,4,((x>>32)&0xFFFFFFFF)); if ((ret=writefd(f,b,8)) != 8) { - fprintf(FERROR,"write_longint failed : %s\n", + rprintf(FERROR,"write_longint failed : %s\n", ret==-1?strerror(errno):"EOF"); exit_cleanup(1); } @@ -416,7 +416,7 @@ void write_buf(int f,char *buf,int len) { int ret; if ((ret=writefd(f,buf,len)) != len) { - fprintf(FERROR,"write_buf failed : %s\n", + rprintf(FERROR,"write_buf failed : %s\n", ret==-1?strerror(errno):"EOF"); exit_cleanup(1); } diff --git a/main.c b/main.c index ccf58955..527daeb6 100644 --- a/main.c +++ b/main.c @@ -57,6 +57,7 @@ int numeric_ids = 0; int force_delete = 0; int io_timeout = 0; int io_error = 0; +static int port = RSYNC_PORT; static char *shell_cmd; @@ -65,8 +66,9 @@ extern int csum_length; int am_server = 0; int am_sender; int recurse = 0; +int am_daemon; -static void usage(FILE *f); +static void usage(int fd); static void report(int f) { @@ -242,10 +244,10 @@ static int do_cmd(char *cmd,char *machine,char *user,char *path,int *f_in,int *f args[argc] = NULL; if (verbose > 3) { - fprintf(FINFO,"cmd="); + rprintf(FINFO,"cmd="); for (i=0;icount > 1) { - fprintf(FERROR,"ERROR: destination must be a directory when copying more than 1 file\n"); + rprintf(FERROR,"ERROR: destination must be a directory when copying more than 1 file\n"); exit_cleanup(1); } return name; @@ -292,14 +294,14 @@ static char *get_local_name(struct file_list *flist,char *name) return NULL; if (do_mkdir(name,0777 & ~orig_umask) != 0) { - fprintf(FERROR,"mkdir %s : %s (1)\n",name,strerror(errno)); + rprintf(FERROR,"mkdir %s : %s (1)\n",name,strerror(errno)); exit_cleanup(1); } else { - fprintf(FINFO,"created directory %s\n",name); + rprintf(FINFO,"created directory %s\n",name); } if (chdir(name) != 0) { - fprintf(FERROR,"chdir %s : %s (2)\n",name,strerror(errno)); + rprintf(FERROR,"chdir %s : %s (2)\n",name,strerror(errno)); exit_cleanup(1); } @@ -309,17 +311,17 @@ static char *get_local_name(struct file_list *flist,char *name) -void do_server_sender(int argc,char *argv[]) +static void do_server_sender(int f_in, int f_out, int argc,char *argv[]) { int i; struct file_list *flist; char *dir = argv[0]; if (verbose > 2) - fprintf(FINFO,"server_sender starting pid=%d\n",(int)getpid()); + rprintf(FINFO,"server_sender starting pid=%d\n",(int)getpid()); if (!relative_paths && chdir(dir) != 0) { - fprintf(FERROR,"chdir %s: %s (3)\n",dir,strerror(errno)); + rprintf(FERROR,"chdir %s: %s (3)\n",dir,strerror(errno)); exit_cleanup(1); } argc--; @@ -340,9 +342,9 @@ void do_server_sender(int argc,char *argv[]) } - flist = send_file_list(STDOUT_FILENO,argc,argv); - send_files(flist,STDOUT_FILENO,STDIN_FILENO); - report(STDOUT_FILENO); + flist = send_file_list(f_out,argc,argv); + send_files(flist,f_out,f_in); + report(f_out); exit_cleanup(0); } @@ -357,7 +359,7 @@ static int do_recv(int f_in,int f_out,struct file_list *flist,char *local_name) init_hard_links(flist); if (pipe(recv_pipe) < 0) { - fprintf(FERROR,"pipe failed in do_recv\n"); + rprintf(FERROR,"pipe failed in do_recv\n"); exit(1); } @@ -365,7 +367,7 @@ static int do_recv(int f_in,int f_out,struct file_list *flist,char *local_name) if ((pid=do_fork()) == 0) { recv_files(f_in,flist,local_name,recv_pipe[1]); if (verbose > 2) - fprintf(FINFO,"receiver read %ld\n",(long)read_total()); + rprintf(FINFO,"receiver read %ld\n",(long)read_total()); exit_cleanup(0); } @@ -377,7 +379,7 @@ static int do_recv(int f_in,int f_out,struct file_list *flist,char *local_name) } -void do_server_recv(int argc,char *argv[]) +static void do_server_recv(int f_in, int f_out, int argc,char *argv[]) { int status; struct file_list *flist; @@ -385,25 +387,25 @@ void do_server_recv(int argc,char *argv[]) char *dir = NULL; if (verbose > 2) - fprintf(FINFO,"server_recv(%d) starting pid=%d\n",argc,(int)getpid()); + rprintf(FINFO,"server_recv(%d) starting pid=%d\n",argc,(int)getpid()); if (argc > 0) { dir = argv[0]; argc--; argv++; if (chdir(dir) != 0) { - fprintf(FERROR,"chdir %s : %s (4)\n", + rprintf(FERROR,"chdir %s : %s (4)\n", dir,strerror(errno)); exit_cleanup(1); } } if (delete_mode) - recv_exclude_list(STDIN_FILENO); + recv_exclude_list(f_in); - flist = recv_file_list(STDIN_FILENO); + flist = recv_file_list(f_in); if (!flist || flist->count == 0) { - fprintf(FERROR,"nothing to do\n"); + rprintf(FERROR,"nothing to do\n"); exit_cleanup(1); } @@ -415,40 +417,114 @@ void do_server_recv(int argc,char *argv[]) local_name = get_local_name(flist,argv[0]); } - status = do_recv(STDIN_FILENO,STDOUT_FILENO,flist,local_name); + status = do_recv(f_in,f_out,flist,local_name); exit_cleanup(status); } -void start_server(int argc, char *argv[]) +void start_server(int f_in, int f_out, int argc, char *argv[]) { - setup_protocol(STDOUT_FILENO,STDIN_FILENO); + setup_protocol(f_out, f_in); if (am_sender) { - recv_exclude_list(STDIN_FILENO); + recv_exclude_list(f_in); if (cvs_exclude) add_cvs_excludes(); - do_server_sender(argc,argv); + do_server_sender(f_in, f_out, argc, argv); } else { - do_server_recv(argc,argv); + do_server_recv(f_in, f_out, argc, argv); } exit_cleanup(0); } +static int client_run(int f_in, int f_out, int pid, int argc, char *argv[]) +{ + struct file_list *flist; + int status = 0, status2 = 0; + char *local_name = NULL; + + setup_protocol(f_out,f_in); + + if (am_sender) { + if (cvs_exclude) + add_cvs_excludes(); + if (delete_mode) + send_exclude_list(f_out); + flist = send_file_list(f_out,argc,argv); + if (verbose > 3) + rprintf(FINFO,"file list sent\n"); + send_files(flist,f_out,f_in); + if (pid != -1) { + if (verbose > 3) + rprintf(FINFO,"waiting on %d\n",pid); + waitpid(pid, &status, 0); + } + report(-1); + exit_cleanup(status); + } + + send_exclude_list(f_out); + + flist = recv_file_list(f_in); + if (!flist || flist->count == 0) { + rprintf(FINFO,"nothing to do\n"); + exit_cleanup(0); + } + + local_name = get_local_name(flist,argv[0]); + + status2 = do_recv(f_in,f_out,flist,local_name); + + report(f_in); + + if (pid != -1) { + waitpid(pid, &status, 0); + } + + return status | status2; +} + + +int start_socket_client(char *host, char *path, int argc, char *argv[]) +{ + int fd; + char *sargs[100]; + int sargc=0; + + fd = open_socket_out(host, port); + if (fd == -1) { + rprintf(FERROR,"failed to connect to %s - %s\n", host, strerror(errno)); + exit_cleanup(1); + } + + server_options(sargs,&sargc); + + sargs[sargc++] = "."; + + if (path && *path) + sargs[sargc++] = path; + + sargs[sargc] = NULL; + + return client_run(fd, fd, -1, argc, argv); +} + int start_client(int argc, char *argv[]) { char *p; char *shell_machine = NULL; char *shell_path = NULL; char *shell_user = NULL; - int pid, status = 0, status2 = 0; + int pid; int f_in,f_out; - struct file_list *flist; - char *local_name = NULL; p = strchr(argv[0],':'); if (p) { + if (p[1] == ':') { + *p = 0; + return start_socket_client(argv[0], p+2, argc-1, argv+1); + } am_sender = 0; *p = 0; shell_machine = argv[0]; @@ -457,12 +533,15 @@ int start_client(int argc, char *argv[]) argv++; } else { am_sender = 1; - + p = strchr(argv[argc-1],':'); if (!p) { local_server = 1; + } else if (p[1] == ':') { + *p = 0; + return start_socket_client(argv[argc-1], p+2, argc-1, argv); } - + if (local_server) { shell_machine = NULL; shell_path = argv[argc-1]; @@ -484,7 +563,7 @@ int start_client(int argc, char *argv[]) } if (verbose > 3) { - fprintf(FINFO,"cmd=%s machine=%s user=%s path=%s\n", + rprintf(FINFO,"cmd=%s machine=%s user=%s path=%s\n", shell_cmd?shell_cmd:"", shell_machine?shell_machine:"", shell_user?shell_user:"", @@ -498,103 +577,65 @@ int start_client(int argc, char *argv[]) pid = do_cmd(shell_cmd,shell_machine,shell_user,shell_path,&f_in,&f_out); - setup_protocol(f_out,f_in); - #if HAVE_SETLINEBUF - setlinebuf(FINFO); - setlinebuf(FERROR); + setlinebuf(stdout); + setlinebuf(stderr); #endif - - if (verbose > 3) - fprintf(FINFO,"parent=%d child=%d sender=%d recurse=%d\n", - (int)getpid(),pid,am_sender,recurse); - - if (am_sender) { - if (cvs_exclude) - add_cvs_excludes(); - if (delete_mode) - send_exclude_list(f_out); - flist = send_file_list(f_out,argc,argv); - if (verbose > 3) - fprintf(FINFO,"file list sent\n"); - send_files(flist,f_out,f_in); - if (verbose > 3) - fprintf(FINFO,"waiting on %d\n",pid); - waitpid(pid, &status, 0); - report(-1); - exit_cleanup(status); - } - - send_exclude_list(f_out); - - flist = recv_file_list(f_in); - if (!flist || flist->count == 0) { - fprintf(FINFO,"nothing to do\n"); - exit_cleanup(0); - } - - local_name = get_local_name(flist,argv[0]); - - status2 = do_recv(f_in,f_out,flist,local_name); - - report(f_in); - - waitpid(pid, &status, 0); - - return status | status2; + + return client_run(f_in, f_out, pid, argc, argv); } -static void usage(FILE *f) +static void usage(int F) { - fprintf(f,"rsync version %s Copyright Andrew Tridgell and Paul Mackerras\n\n", + rprintf(F,"rsync version %s Copyright Andrew Tridgell and Paul Mackerras\n\n", VERSION); - fprintf(f,"Usage:\t%s [options] src user@host:dest\nOR",RSYNC_NAME); - fprintf(f,"\t%s [options] user@host:src dest\n\n",RSYNC_NAME); - fprintf(f,"Options:\n"); - fprintf(f,"-v, --verbose increase verbosity\n"); - fprintf(f,"-c, --checksum always checksum\n"); - fprintf(f,"-a, --archive archive mode (same as -rlptDog)\n"); - fprintf(f,"-r, --recursive recurse into directories\n"); - fprintf(f,"-R, --relative use relative path names\n"); - fprintf(f,"-b, --backup make backups (default ~ extension)\n"); - fprintf(f,"-u, --update update only (don't overwrite newer files)\n"); - fprintf(f,"-l, --links preserve soft links\n"); - fprintf(f,"-L, --copy-links treat soft links like regular files\n"); - fprintf(f,"-H, --hard-links preserve hard links\n"); - fprintf(f,"-p, --perms preserve permissions\n"); - fprintf(f,"-o, --owner preserve owner (root only)\n"); - fprintf(f,"-g, --group preserve group\n"); - fprintf(f,"-D, --devices preserve devices (root only)\n"); - fprintf(f,"-t, --times preserve times\n"); - fprintf(f,"-S, --sparse handle sparse files efficiently\n"); - fprintf(f,"-n, --dry-run show what would have been transferred\n"); - fprintf(f,"-W, --whole-file copy whole files, no incremental checks\n"); - fprintf(f,"-x, --one-file-system don't cross filesystem boundaries\n"); - fprintf(f,"-B, --block-size SIZE checksum blocking size\n"); - fprintf(f,"-e, --rsh COMMAND specify rsh replacement\n"); - fprintf(f," --rsync-path PATH specify path to rsync on the remote machine\n"); - fprintf(f,"-C, --cvs-exclude auto ignore files in the same way CVS does\n"); - fprintf(f," --delete delete files that don't exist on the sending side\n"); - fprintf(f," --force force deletion of directories even if not empty\n"); - fprintf(f," --numeric-ids don't map uid/gid values by user/group name\n"); - fprintf(f," --timeout TIME set IO timeout in seconds\n"); - fprintf(f,"-I, --ignore-times don't exclude files that match length and time\n"); - fprintf(f,"-T --temp-dir DIR create temporary files in directory DIR\n"); - fprintf(f,"-z, --compress compress file data\n"); - fprintf(f," --exclude FILE exclude file FILE\n"); - fprintf(f," --exclude-from FILE exclude files listed in FILE\n"); - fprintf(f," --suffix SUFFIX override backup suffix\n"); - fprintf(f," --version print version number\n"); - - fprintf(f,"\n"); - fprintf(f,"the backup suffix defaults to %s\n",BACKUP_SUFFIX); - fprintf(f,"the block size defaults to %d\n",BLOCK_SIZE); + rprintf(F,"Usage:\t%s [options] src user@host:dest\nOR",RSYNC_NAME); + rprintf(F,"\t%s [options] user@host:src dest\n\n",RSYNC_NAME); + rprintf(F,"Options:\n"); + rprintf(F,"-v, --verbose increase verbosity\n"); + rprintf(F,"-c, --checksum always checksum\n"); + rprintf(F,"-a, --archive archive mode (same as -rlptDog)\n"); + rprintf(F,"-r, --recursive recurse into directories\n"); + rprintf(F,"-R, --relative use relative path names\n"); + rprintf(F,"-b, --backup make backups (default ~ extension)\n"); + rprintf(F,"-u, --update update only (don't overwrite newer files)\n"); + rprintf(F,"-l, --links preserve soft links\n"); + rprintf(F,"-L, --copy-links treat soft links like regular files\n"); + rprintf(F,"-H, --hard-links preserve hard links\n"); + rprintf(F,"-p, --perms preserve permissions\n"); + rprintf(F,"-o, --owner preserve owner (root only)\n"); + rprintf(F,"-g, --group preserve group\n"); + rprintf(F,"-D, --devices preserve devices (root only)\n"); + rprintf(F,"-t, --times preserve times\n"); + rprintf(F,"-S, --sparse handle sparse files efficiently\n"); + rprintf(F,"-n, --dry-run show what would have been transferred\n"); + rprintf(F,"-W, --whole-file copy whole files, no incremental checks\n"); + rprintf(F,"-x, --one-file-system don't cross filesystem boundaries\n"); + rprintf(F,"-B, --block-size SIZE checksum blocking size\n"); + rprintf(F,"-e, --rsh COMMAND specify rsh replacement\n"); + rprintf(F," --rsync-path PATH specify path to rsync on the remote machine\n"); + rprintf(F,"-C, --cvs-exclude auto ignore files in the same way CVS does\n"); + rprintf(F," --delete delete files that don't exist on the sending side\n"); + rprintf(F," --force force deletion of directories even if not empty\n"); + rprintf(F," --numeric-ids don't map uid/gid values by user/group name\n"); + rprintf(F," --timeout TIME set IO timeout in seconds\n"); + rprintf(F,"-I, --ignore-times don't exclude files that match length and time\n"); + rprintf(F,"-T --temp-dir DIR create temporary files in directory DIR\n"); + rprintf(F,"-z, --compress compress file data\n"); + rprintf(F," --exclude FILE exclude file FILE\n"); + rprintf(F," --exclude-from FILE exclude files listed in FILE\n"); + rprintf(F," --suffix SUFFIX override backup suffix\n"); + rprintf(F," --version print version number\n"); + + rprintf(F,"\n"); + rprintf(F,"the backup suffix defaults to %s\n",BACKUP_SUFFIX); + rprintf(F,"the block size defaults to %d\n",BLOCK_SIZE); } enum {OPT_VERSION,OPT_SUFFIX,OPT_SENDER,OPT_SERVER,OPT_EXCLUDE, OPT_EXCLUDE_FROM,OPT_DELETE,OPT_NUMERIC_IDS,OPT_RSYNC_PATH, - OPT_FORCE,OPT_TIMEOUT}; + OPT_FORCE,OPT_TIMEOUT,OPT_DAEMON}; static char *short_options = "oblLWHpguDCtcahvrRIxnSe:B:T:z"; @@ -636,6 +677,7 @@ static struct option long_options[] = { {"timeout", 1, 0, OPT_TIMEOUT}, {"temp-dir", 1, 0, 'T'}, {"compress", 0, 0, 'z'}, + {"daemon", 0, 0, OPT_DAEMON}, {0,0,0,0}}; RETSIGTYPE sigusr1_handler(int val) { @@ -734,7 +776,7 @@ static void parse_arguments(int argc, char *argv[]) #if SUPPORT_HARD_LINKS preserve_hard_links=1; #else - fprintf(FERROR,"ERROR: hard links not supported on this platform\n"); + rprintf(FERROR,"ERROR: hard links not supported on this platform\n"); exit_cleanup(1); #endif break; @@ -821,8 +863,12 @@ static void parse_arguments(int argc, char *argv[]) do_compression = 1; break; + case OPT_DAEMON: + am_daemon = 1; + break; + default: - /* fprintf(FERROR,"bad option -%c\n",opt); */ + /* rprintf(FERROR,"bad option -%c\n",opt); */ exit_cleanup(1); } } @@ -858,13 +904,13 @@ int main(int argc,char *argv[]) #ifndef SUPPORT_LINKS if (!am_server && preserve_links) { - fprintf(FERROR,"ERROR: symbolic links not supported\n"); + rprintf(FERROR,"ERROR: symbolic links not supported\n"); exit_cleanup(1); } #endif if (am_server) { - start_server(argc, argv); + start_server(STDIN_FILENO, STDOUT_FILENO, argc, argv); } if (argc < 2) { diff --git a/match.c b/match.c index 3689af4f..bc759e24 100644 --- a/match.c +++ b/match.c @@ -97,7 +97,7 @@ static void matched(int f,struct sum_struct *s,struct map_struct *buf, int j; if (verbose > 2 && i >= 0) - fprintf(FINFO,"match at %d last_match=%d j=%d len=%d n=%d\n", + rprintf(FINFO,"match at %d last_match=%d j=%d len=%d n=%d\n", (int)offset,(int)last_match,i,(int)s->sums[i].len,(int)n); send_token(f,i,buf,last_match,n,i==-1?0:s->sums[i].len); @@ -133,7 +133,7 @@ static void hash_search(int f,struct sum_struct *s, schar *map; if (verbose > 2) - fprintf(FINFO,"hash search b=%d len=%d\n",s->n,(int)len); + rprintf(FINFO,"hash search b=%d len=%d\n",s->n,(int)len); k = MIN(len, s->n); @@ -143,14 +143,14 @@ static void hash_search(int f,struct sum_struct *s, s1 = sum & 0xFFFF; s2 = sum >> 16; if (verbose > 3) - fprintf(FINFO, "sum=%.8x k=%d\n", sum, k); + rprintf(FINFO, "sum=%.8x k=%d\n", sum, k); offset = 0; end = len + 1 - s->sums[s->count-1].len; if (verbose > 3) - fprintf(FINFO,"hash search s->n=%d len=%d count=%d\n", + rprintf(FINFO,"hash search s->n=%d len=%d count=%d\n", s->n,(int)len,s->count); do { @@ -159,7 +159,7 @@ static void hash_search(int f,struct sum_struct *s, j = tag_table[t]; if (verbose > 4) - fprintf(FINFO,"offset=%d sum=%08x\n",(int)offset,sum); + rprintf(FINFO,"offset=%d sum=%08x\n",(int)offset,sum); if (j == NULL_TAG) { goto null_tag; @@ -173,7 +173,7 @@ static void hash_search(int f,struct sum_struct *s, if (sum != s->sums[i].sum1) continue; if (verbose > 3) - fprintf(FINFO,"potential match at %d target=%d %d sum=%08x\n", + rprintf(FINFO,"potential match at %d target=%d %d sum=%08x\n", (int)offset,j,i,sum); if (!done_csum2) { @@ -236,12 +236,12 @@ void match_sums(int f,struct sum_struct *s,struct map_struct *buf,OFF_T len) build_hash_table(s); if (verbose > 2) - fprintf(FINFO,"built hash table\n"); + rprintf(FINFO,"built hash table\n"); hash_search(f,s,buf,len); if (verbose > 2) - fprintf(FINFO,"done hash search\n"); + rprintf(FINFO,"done hash search\n"); } else { matched(f,s,buf,len,-1); } @@ -250,7 +250,7 @@ void match_sums(int f,struct sum_struct *s,struct map_struct *buf,OFF_T len) if (remote_version >= 14) { if (verbose > 2) - fprintf(FINFO,"sending file_sum\n"); + rprintf(FINFO,"sending file_sum\n"); write_buf(f,file_sum,MD4_SUM_LENGTH); } @@ -260,7 +260,7 @@ void match_sums(int f,struct sum_struct *s,struct map_struct *buf,OFF_T len) } if (verbose > 2) - fprintf(FINFO, "false_alarms=%d tag_hits=%d matches=%d\n", + rprintf(FINFO, "false_alarms=%d tag_hits=%d matches=%d\n", false_alarms, tag_hits, matches); total_tag_hits += tag_hits; @@ -274,7 +274,7 @@ void match_report(void) if (verbose <= 1) return; - fprintf(FINFO, + rprintf(FINFO, "total: matches=%d tag_hits=%d false_alarms=%d data=%ld\n", total_matches,total_tag_hits, total_false_alarms,(long)total_data_transfer); diff --git a/md4.c b/md4.c index 35d160d1..a12a5ded 100644 --- a/md4.c +++ b/md4.c @@ -213,7 +213,7 @@ if (count == 0 && MDp->done) return; /* check to see if MD is already done and report error */ if (MDp->done) - { fprintf(FERROR,"\nError: MDupdate MD already done."); return; } + { rprintf(FERROR,"\nError: MDupdate MD already done."); return; } /* Add count to MDp->count */ tmp = count; p = MDp->count; @@ -228,7 +228,7 @@ MDblock(MDp,(unsigned int *)X); } else if (count > 512) /* Check for count too large */ - { fprintf(FERROR,"\nError: MDupdate called with illegal count value %d." + { rprintf(FERROR,"\nError: MDupdate called with illegal count value %d." ,count); return; } diff --git a/rsync.c b/rsync.c index 16a7a31b..9d3b1c56 100644 --- a/rsync.c +++ b/rsync.c @@ -83,25 +83,25 @@ static int delete_file(char *fname) ret = do_stat(fname, &st); #endif if (ret) { - fprintf(FERROR,"stat(%s) : %s\n", fname, strerror(errno)); + rprintf(FERROR,"stat(%s) : %s\n", fname, strerror(errno)); return -1; } if (!S_ISDIR(st.st_mode)) { - fprintf(FERROR,"unlink(%s) : %s\n", fname, strerror(errno)); + rprintf(FERROR,"unlink(%s) : %s\n", fname, strerror(errno)); return -1; } if (do_rmdir(fname) == 0 || errno == ENOENT) return 0; if (!force_delete || (errno != ENOTEMPTY && errno != EEXIST)) { - fprintf(FERROR,"rmdir(%s) : %s\n", fname, strerror(errno)); + rprintf(FERROR,"rmdir(%s) : %s\n", fname, strerror(errno)); return -1; } /* now we do a recsursive delete on the directory ... */ d = opendir(fname); if (!d) { - fprintf(FERROR,"opendir(%s): %s\n", + rprintf(FERROR,"opendir(%s): %s\n", fname,strerror(errno)); return -1; } @@ -116,7 +116,7 @@ static int delete_file(char *fname) strcat(buf, dname); buf[MAXPATHLEN-1] = 0; if (verbose > 0) - fprintf(FINFO,"deleting %s\n", buf); + rprintf(FINFO,"deleting %s\n", buf); if (delete_file(buf) != 0) { closedir(d); return -1; @@ -126,7 +126,7 @@ static int delete_file(char *fname) closedir(d); if (do_rmdir(fname) != 0) { - fprintf(FERROR,"rmdir(%s) : %s\n", fname, strerror(errno)); + rprintf(FERROR,"rmdir(%s) : %s\n", fname, strerror(errno)); return -1; } @@ -184,7 +184,7 @@ static struct sum_struct *generate_sums(struct map_struct *buf,OFF_T len,int n) } if (verbose > 3) - fprintf(FINFO,"count=%d rem=%d n=%d flength=%d\n", + rprintf(FINFO,"count=%d rem=%d n=%d flength=%d\n", s->count,s->remainder,s->n,(int)s->flength); s->sums = (struct sum_buf *)malloc(sizeof(s->sums[0])*s->count); @@ -202,7 +202,7 @@ static struct sum_struct *generate_sums(struct map_struct *buf,OFF_T len,int n) s->sums[i].i = i; if (verbose > 3) - fprintf(FINFO,"chunk[%d] offset=%d len=%d sum1=%08x\n", + rprintf(FINFO,"chunk[%d] offset=%d len=%d sum1=%08x\n", i,(int)s->sums[i].offset,s->sums[i].len,s->sums[i].sum1); len -= n1; @@ -231,7 +231,7 @@ static struct sum_struct *receive_sums(int f) s->sums = NULL; if (verbose > 3) - fprintf(FINFO,"count=%d n=%d rem=%d\n", + rprintf(FINFO,"count=%d n=%d rem=%d\n", s->count,s->n,s->remainder); if (s->count == 0) @@ -255,7 +255,7 @@ static struct sum_struct *receive_sums(int f) offset += s->sums[i].len; if (verbose > 3) - fprintf(FINFO,"chunk[%d] len=%d offset=%d sum1=%08x\n", + rprintf(FINFO,"chunk[%d] len=%d offset=%d sum1=%08x\n", i,s->sums[i].len,(int)s->sums[i].offset,s->sums[i].sum1); } @@ -275,7 +275,7 @@ static int set_perms(char *fname,struct file_struct *file,STRUCT_STAT *st, if (!st) { if (link_stat(fname,&st2) != 0) { - fprintf(FERROR,"stat %s : %s\n",fname,strerror(errno)); + rprintf(FERROR,"stat %s : %s\n",fname,strerror(errno)); return 0; } st = &st2; @@ -285,7 +285,7 @@ static int set_perms(char *fname,struct file_struct *file,STRUCT_STAT *st, st->st_mtime != file->modtime) { updated = 1; if (set_modtime(fname,file->modtime) != 0) { - fprintf(FERROR,"failed to set times on %s : %s\n", + rprintf(FERROR,"failed to set times on %s : %s\n", fname,strerror(errno)); return 0; } @@ -296,7 +296,7 @@ static int set_perms(char *fname,struct file_struct *file,STRUCT_STAT *st, st->st_mode != file->mode) { updated = 1; if (do_chmod(fname,file->mode) != 0) { - fprintf(FERROR,"failed to set permissions on %s : %s\n", + rprintf(FERROR,"failed to set permissions on %s : %s\n", fname,strerror(errno)); return 0; } @@ -311,7 +311,7 @@ static int set_perms(char *fname,struct file_struct *file,STRUCT_STAT *st, if (preserve_uid && st->st_uid != file->uid) updated = 1; if (verbose>1 || preserve_uid) - fprintf(FERROR,"chown %s : %s\n", + rprintf(FERROR,"chown %s : %s\n", fname,strerror(errno)); return updated; } @@ -320,9 +320,9 @@ static int set_perms(char *fname,struct file_struct *file,STRUCT_STAT *st, if (verbose > 1 && report) { if (updated) - fprintf(FINFO,"%s\n",fname); + rprintf(FINFO,"%s\n",fname); else - fprintf(FINFO,"%s is uptodate\n",fname); + rprintf(FINFO,"%s is uptodate\n",fname); } return updated; } @@ -372,7 +372,7 @@ void recv_generator(char *fname,struct file_list *flist,int i,int f_out) struct file_struct *file = flist->files[i]; if (verbose > 2) - fprintf(FINFO,"recv_generator(%s,%d)\n",fname,i); + rprintf(FINFO,"recv_generator(%s,%d)\n",fname,i); statret = link_stat(fname,&st); @@ -380,7 +380,7 @@ void recv_generator(char *fname,struct file_list *flist,int i,int f_out) if (dry_run) return; if (statret == 0 && !S_ISDIR(st.st_mode)) { if (do_unlink(fname) != 0) { - fprintf(FERROR,"unlink %s : %s\n",fname,strerror(errno)); + rprintf(FERROR,"unlink %s : %s\n",fname,strerror(errno)); return; } statret = -1; @@ -389,12 +389,12 @@ void recv_generator(char *fname,struct file_list *flist,int i,int f_out) if (!(relative_paths && errno==ENOENT && create_directory_path(fname)==0 && do_mkdir(fname,file->mode)==0)) { - fprintf(FERROR,"mkdir %s : %s (2)\n", + rprintf(FERROR,"mkdir %s : %s (2)\n", fname,strerror(errno)); } } if (set_perms(fname,file,NULL,0) && verbose) - fprintf(FINFO,"%s/\n",fname); + rprintf(FINFO,"%s/\n",fname); return; } @@ -414,12 +414,12 @@ void recv_generator(char *fname,struct file_list *flist,int i,int f_out) } delete_file(fname); if (do_symlink(file->link,fname) != 0) { - fprintf(FERROR,"link %s -> %s : %s\n", + rprintf(FERROR,"link %s -> %s : %s\n", fname,file->link,strerror(errno)); } else { set_perms(fname,file,NULL,0); if (verbose) - fprintf(FINFO,"%s -> %s\n", + rprintf(FINFO,"%s -> %s\n", fname,file->link); } #endif @@ -433,14 +433,14 @@ void recv_generator(char *fname,struct file_list *flist,int i,int f_out) st.st_rdev != file->rdev) { delete_file(fname); if (verbose > 2) - fprintf(FINFO,"mknod(%s,0%o,0x%x)\n", + rprintf(FINFO,"mknod(%s,0%o,0x%x)\n", fname,(int)file->mode,(int)file->rdev); if (do_mknod(fname,file->mode,file->rdev) != 0) { - fprintf(FERROR,"mknod %s : %s\n",fname,strerror(errno)); + rprintf(FERROR,"mknod %s : %s\n",fname,strerror(errno)); } else { set_perms(fname,file,NULL,0); if (verbose) - fprintf(FINFO,"%s\n",fname); + rprintf(FINFO,"%s\n",fname); } } else { set_perms(fname,file,&st,1); @@ -451,12 +451,12 @@ void recv_generator(char *fname,struct file_list *flist,int i,int f_out) if (preserve_hard_links && check_hard_link(file)) { if (verbose > 1) - fprintf(FINFO,"%s is a hard link\n",f_name(file)); + rprintf(FINFO,"%s is a hard link\n",f_name(file)); return; } if (!S_ISREG(file->mode)) { - fprintf(FINFO,"skipping non-regular file %s\n",fname); + rprintf(FINFO,"skipping non-regular file %s\n",fname); return; } @@ -466,7 +466,7 @@ void recv_generator(char *fname,struct file_list *flist,int i,int f_out) if (!dry_run) send_sums(NULL,f_out); } else { if (verbose > 1) - fprintf(FERROR,"recv_generator failed to open %s\n",fname); + rprintf(FERROR,"recv_generator failed to open %s\n",fname); } return; } @@ -484,7 +484,7 @@ void recv_generator(char *fname,struct file_list *flist,int i,int f_out) if (update_only && st.st_mtime > file->modtime) { if (verbose > 1) - fprintf(FINFO,"%s is newer\n",fname); + rprintf(FINFO,"%s is newer\n",fname); return; } @@ -508,8 +508,8 @@ void recv_generator(char *fname,struct file_list *flist,int i,int f_out) fd = open(fname,O_RDONLY); if (fd == -1) { - fprintf(FERROR,"failed to open %s : %s\n",fname,strerror(errno)); - fprintf(FERROR,"skipping %s\n",fname); + rprintf(FERROR,"failed to open %s : %s\n",fname,strerror(errno)); + rprintf(FERROR,"skipping %s\n",fname); return; } @@ -520,12 +520,12 @@ void recv_generator(char *fname,struct file_list *flist,int i,int f_out) } if (verbose > 3) - fprintf(FINFO,"gen mapped %s of size %d\n",fname,(int)st.st_size); + rprintf(FINFO,"gen mapped %s of size %d\n",fname,(int)st.st_size); s = generate_sums(buf,st.st_size,adapt_block_size(file, block_size)); if (verbose > 2) - fprintf(FINFO,"sending sums for %d\n",i); + rprintf(FINFO,"sending sums for %d\n",i); write_int(f_out,i); send_sums(s,f_out); @@ -558,12 +558,12 @@ static int receive_data(int f_in,struct map_struct *buf,int fd,char *fname) for (i=recv_token(f_in,&data); i != 0; i=recv_token(f_in,&data)) { if (i > 0) { if (verbose > 3) - fprintf(FINFO,"data recv %d at %d\n",i,(int)offset); + rprintf(FINFO,"data recv %d at %d\n",i,(int)offset); sum_update(data,i); if (fd != -1 && write_file(fd,data,i) != i) { - fprintf(FERROR,"write failed on %s : %s\n",fname,strerror(errno)); + rprintf(FERROR,"write failed on %s : %s\n",fname,strerror(errno)); exit_cleanup(1); } offset += i; @@ -575,7 +575,7 @@ static int receive_data(int f_in,struct map_struct *buf,int fd,char *fname) len = remainder; if (verbose > 3) - fprintf(FINFO,"chunk[%d] of size %d at %d offset=%d\n", + rprintf(FINFO,"chunk[%d] of size %d at %d offset=%d\n", i,len,(int)offset2,(int)offset); map = map_ptr(buf,offset2,len); @@ -584,7 +584,7 @@ static int receive_data(int f_in,struct map_struct *buf,int fd,char *fname) sum_update(map,len); if (fd != -1 && write_file(fd,map,len) != len) { - fprintf(FERROR,"write failed on %s : %s\n",fname,strerror(errno)); + rprintf(FERROR,"write failed on %s : %s\n",fname,strerror(errno)); exit_cleanup(1); } offset += len; @@ -592,7 +592,7 @@ static int receive_data(int f_in,struct map_struct *buf,int fd,char *fname) } if (fd != -1 && offset > 0 && sparse_end(fd) != 0) { - fprintf(FERROR,"write failed on %s : %s\n",fname,strerror(errno)); + rprintf(FERROR,"write failed on %s : %s\n",fname,strerror(errno)); exit_cleanup(1); } @@ -601,7 +601,7 @@ static int receive_data(int f_in,struct map_struct *buf,int fd,char *fname) if (remote_version >= 14) { read_buf(f_in,file_sum2,MD4_SUM_LENGTH); if (verbose > 2) - fprintf(FINFO,"got file_sum\n"); + rprintf(FINFO,"got file_sum\n"); if (fd != -1 && memcmp(file_sum1,file_sum2,MD4_SUM_LENGTH) != 0) return 0; } @@ -613,16 +613,16 @@ static void delete_one(struct file_struct *f) { if (!S_ISDIR(f->mode)) { if (do_unlink(f_name(f)) != 0) { - fprintf(FERROR,"unlink %s : %s\n",f_name(f),strerror(errno)); + rprintf(FERROR,"unlink %s : %s\n",f_name(f),strerror(errno)); } else if (verbose) { - fprintf(FINFO,"deleting %s\n",f_name(f)); + rprintf(FINFO,"deleting %s\n",f_name(f)); } } else { if (do_rmdir(f_name(f)) != 0) { if (errno != ENOTEMPTY && errno != EEXIST) - fprintf(FERROR,"rmdir %s : %s\n",f_name(f),strerror(errno)); + rprintf(FERROR,"rmdir %s : %s\n",f_name(f),strerror(errno)); } else if (verbose) { - fprintf(FINFO,"deleting directory %s\n",f_name(f)); + rprintf(FINFO,"deleting directory %s\n",f_name(f)); } } } @@ -652,7 +652,7 @@ static void add_delete_entry(struct file_struct *file) dlist_len++; if (verbose > 3) - fprintf(FINFO,"added %s to delete list\n", f_name(file)); + rprintf(FINFO,"added %s to delete list\n", f_name(file)); } /* yuck! This function wouldn't have been necessary if I had the sorting @@ -689,7 +689,7 @@ static void delete_files(struct file_list *flist) add_cvs_excludes(); if (io_error) { - fprintf(FINFO,"IO error encountered - skipping file deletion\n"); + rprintf(FINFO,"IO error encountered - skipping file deletion\n"); return; } @@ -707,7 +707,7 @@ static void delete_files(struct file_list *flist) } if (verbose > 1) - fprintf(FINFO,"deleting in %s\n", name); + rprintf(FINFO,"deleting in %s\n", name); for (i=local_file_list->count-1;i>=0;i--) { if (!local_file_list->files[i]->basename) continue; @@ -754,7 +754,7 @@ int recv_files(int f_in,struct file_list *flist,char *local_name,int f_gen) int recv_ok; if (verbose > 2) { - fprintf(FINFO,"recv_files(%d) starting\n",flist->count); + rprintf(FINFO,"recv_files(%d) starting\n",flist->count); } if (recurse && delete_mode && !local_name && flist->count>0) { @@ -769,7 +769,7 @@ int recv_files(int f_in,struct file_list *flist,char *local_name,int f_gen) phase++; csum_length = SUM_LENGTH; if (verbose > 2) - fprintf(FINFO,"recv_files phase=%d\n",phase); + rprintf(FINFO,"recv_files phase=%d\n",phase); write_int(f_gen,-1); write_flush(f_gen); continue; @@ -790,20 +790,20 @@ int recv_files(int f_in,struct file_list *flist,char *local_name,int f_gen) } if (verbose > 2) - fprintf(FINFO,"recv_files(%s)\n",fname); + rprintf(FINFO,"recv_files(%s)\n",fname); /* open the file */ fd1 = open(fname,O_RDONLY); if (fd1 != -1 && do_fstat(fd1,&st) != 0) { - fprintf(FERROR,"fstat %s : %s\n",fname,strerror(errno)); + rprintf(FERROR,"fstat %s : %s\n",fname,strerror(errno)); receive_data(f_in,NULL,-1,NULL); close(fd1); continue; } if (fd1 != -1 && !S_ISREG(st.st_mode)) { - fprintf(FERROR,"%s : not a regular file (recv_files)\n",fname); + rprintf(FERROR,"%s : not a regular file (recv_files)\n",fname); receive_data(f_in,NULL,-1,NULL); close(fd1); continue; @@ -812,14 +812,14 @@ int recv_files(int f_in,struct file_list *flist,char *local_name,int f_gen) if (fd1 != -1 && st.st_size > 0) { buf = map_file(fd1,st.st_size); if (verbose > 2) - fprintf(FINFO,"recv mapped %s of size %d\n",fname,(int)st.st_size); + rprintf(FINFO,"recv mapped %s of size %d\n",fname,(int)st.st_size); } else { buf = NULL; } /* open tmp file */ if (strlen(fname) > (MAXPATHLEN-8)) { - fprintf(FERROR,"filename too long\n"); + rprintf(FERROR,"filename too long\n"); if (buf) unmap_file(buf); close(fd1); continue; @@ -836,7 +836,7 @@ int recv_files(int f_in,struct file_list *flist,char *local_name,int f_gen) sprintf(fnametmp,"%s.XXXXXX",fname); } if (NULL == do_mktemp(fnametmp)) { - fprintf(FERROR,"mktemp %s failed\n",fnametmp); + rprintf(FERROR,"mktemp %s failed\n",fnametmp); receive_data(f_in,buf,-1,NULL); if (buf) unmap_file(buf); close(fd1); @@ -848,7 +848,7 @@ int recv_files(int f_in,struct file_list *flist,char *local_name,int f_gen) fd2 = do_open(fnametmp,O_WRONLY|O_CREAT|O_EXCL,file->mode); } if (fd2 == -1) { - fprintf(FERROR,"open %s : %s\n",fnametmp,strerror(errno)); + rprintf(FERROR,"open %s : %s\n",fnametmp,strerror(errno)); receive_data(f_in,buf,-1,NULL); if (buf) unmap_file(buf); close(fd1); @@ -870,17 +870,17 @@ int recv_files(int f_in,struct file_list *flist,char *local_name,int f_gen) close(fd2); if (verbose > 2) - fprintf(FINFO,"renaming %s to %s\n",fnametmp,fname); + rprintf(FINFO,"renaming %s to %s\n",fnametmp,fname); if (make_backups) { char fnamebak[MAXPATHLEN]; if (strlen(fname) + strlen(backup_suffix) > (MAXPATHLEN-1)) { - fprintf(FERROR,"backup filename too long\n"); + rprintf(FERROR,"backup filename too long\n"); continue; } sprintf(fnamebak,"%s%s",fname,backup_suffix); if (do_rename(fname,fnamebak) != 0 && errno != ENOENT) { - fprintf(FERROR,"rename %s %s : %s\n",fname,fnamebak,strerror(errno)); + rprintf(FERROR,"rename %s %s : %s\n",fname,fnamebak,strerror(errno)); continue; } } @@ -891,14 +891,14 @@ int recv_files(int f_in,struct file_list *flist,char *local_name,int f_gen) /* rename failed on cross-filesystem link. Copy the file instead. */ if (copy_file(fnametmp,fname, file->mode)) { - fprintf(FERROR,"copy %s -> %s : %s\n", + rprintf(FERROR,"copy %s -> %s : %s\n", fnametmp,fname,strerror(errno)); } else { set_perms(fname,file,NULL,0); } do_unlink(fnametmp); } else { - fprintf(FERROR,"rename %s -> %s : %s\n", + rprintf(FERROR,"rename %s -> %s : %s\n", fnametmp,fname,strerror(errno)); do_unlink(fnametmp); } @@ -911,11 +911,11 @@ int recv_files(int f_in,struct file_list *flist,char *local_name,int f_gen) if (!recv_ok) { if (csum_length == SUM_LENGTH) { - fprintf(FERROR,"ERROR: file corruption in %s. File changed during transfer?\n", + rprintf(FERROR,"ERROR: file corruption in %s. File changed during transfer?\n", fname); } else { if (verbose > 1) - fprintf(FINFO,"redoing %s(%d)\n",fname,i); + rprintf(FINFO,"redoing %s(%d)\n",fname,i); write_int(f_gen,i); } } @@ -933,7 +933,7 @@ int recv_files(int f_in,struct file_list *flist,char *local_name,int f_gen) } if (verbose > 2) - fprintf(FINFO,"recv_files finished\n"); + rprintf(FINFO,"recv_files finished\n"); return 0; } @@ -953,7 +953,7 @@ void send_files(struct file_list *flist,int f_out,int f_in) int offset=0; if (verbose > 2) - fprintf(FINFO,"send_files starting\n"); + rprintf(FINFO,"send_files starting\n"); setup_nonblocking(f_in,f_out); @@ -966,7 +966,7 @@ void send_files(struct file_list *flist,int f_out,int f_in) write_int(f_out,-1); write_flush(f_out); if (verbose > 2) - fprintf(FINFO,"send_files phase=%d\n",phase); + rprintf(FINFO,"send_files phase=%d\n",phase); continue; } break; @@ -980,7 +980,7 @@ void send_files(struct file_list *flist,int f_out,int f_in) fname[MAXPATHLEN-1] = 0; if (strlen(fname) == MAXPATHLEN-1) { io_error = 1; - fprintf(FERROR, "send_files failed on long-named directory %s\n", + rprintf(FERROR, "send_files failed on long-named directory %s\n", fname); return; } @@ -990,7 +990,7 @@ void send_files(struct file_list *flist,int f_out,int f_in) strncat(fname,f_name(file),MAXPATHLEN-strlen(fname)); if (verbose > 2) - fprintf(FINFO,"send_files(%d,%s)\n",i,fname); + rprintf(FINFO,"send_files(%d,%s)\n",i,fname); if (dry_run) { if (!am_server && verbose) @@ -1002,14 +1002,14 @@ void send_files(struct file_list *flist,int f_out,int f_in) s = receive_sums(f_in); if (!s) { io_error = 1; - fprintf(FERROR,"receive_sums failed\n"); + rprintf(FERROR,"receive_sums failed\n"); return; } fd = open(fname,O_RDONLY); if (fd == -1) { io_error = 1; - fprintf(FERROR,"send_files failed to open %s: %s\n", + rprintf(FERROR,"send_files failed to open %s: %s\n", fname,strerror(errno)); free_sums(s); continue; @@ -1018,7 +1018,7 @@ void send_files(struct file_list *flist,int f_out,int f_in) /* map the local file */ if (do_fstat(fd,&st) != 0) { io_error = 1; - fprintf(FERROR,"fstat failed : %s\n",strerror(errno)); + rprintf(FERROR,"fstat failed : %s\n",strerror(errno)); free_sums(s); close(fd); return; @@ -1031,7 +1031,7 @@ void send_files(struct file_list *flist,int f_out,int f_in) } if (verbose > 2) - fprintf(FINFO,"send_files mapped %s of size %d\n", + rprintf(FINFO,"send_files mapped %s of size %d\n", fname,(int)st.st_size); write_int(f_out,i); @@ -1041,7 +1041,7 @@ void send_files(struct file_list *flist,int f_out,int f_in) write_int(f_out,s->remainder); if (verbose > 2) - fprintf(FINFO,"calling match_sums %s\n",fname); + rprintf(FINFO,"calling match_sums %s\n",fname); if (!am_server && verbose) printf("%s\n",fname+offset); @@ -1055,11 +1055,11 @@ void send_files(struct file_list *flist,int f_out,int f_in) free_sums(s); if (verbose > 2) - fprintf(FINFO,"sender finished %s\n",fname); + rprintf(FINFO,"sender finished %s\n",fname); } if (verbose > 2) - fprintf(FINFO,"send files finished\n"); + rprintf(FINFO,"send files finished\n"); match_report(); @@ -1075,7 +1075,7 @@ void generate_files(int f,struct file_list *flist,char *local_name,int f_recv) int phase=0; if (verbose > 2) - fprintf(FINFO,"generator starting pid=%d count=%d\n", + rprintf(FINFO,"generator starting pid=%d count=%d\n", (int)getpid(),flist->count); for (i = 0; i < flist->count; i++) { @@ -1101,7 +1101,7 @@ void generate_files(int f,struct file_list *flist,char *local_name,int f_recv) ignore_times=1; if (verbose > 2) - fprintf(FINFO,"generate_files phase=%d\n",phase); + rprintf(FINFO,"generate_files phase=%d\n",phase); write_int(f,-1); write_flush(f); @@ -1121,7 +1121,7 @@ void generate_files(int f,struct file_list *flist,char *local_name,int f_recv) phase++; if (verbose > 2) - fprintf(FINFO,"generate_files phase=%d\n",phase); + rprintf(FINFO,"generate_files phase=%d\n",phase); write_int(f,-1); write_flush(f); @@ -1129,7 +1129,7 @@ void generate_files(int f,struct file_list *flist,char *local_name,int f_recv) if (verbose > 2) - fprintf(FINFO,"generator wrote %ld\n",(long)write_total()); + rprintf(FINFO,"generator wrote %ld\n",(long)write_total()); } diff --git a/rsync.h b/rsync.h index 14910462..e5206409 100644 --- a/rsync.h +++ b/rsync.h @@ -43,6 +43,8 @@ #define MIN_PROTOCOL_VERSION 11 #define MAX_PROTOCOL_VERSION 30 +#define RSYNC_PORT 873 + #define SPARSE_WRITE_SIZE (1024) #define WRITE_SIZE (32*1024) #define CHUNK_SIZE (32*1024) @@ -50,8 +52,8 @@ #define BLOCKING_TIMEOUT 10 -#define FERROR stderr -#define FINFO (am_server?stderr:stdout) +#define FERROR 1 +#define FINFO 2 #include "config.h" @@ -167,6 +169,8 @@ #include #include +#include + #ifndef S_IFLNK #define S_IFLNK 0120000 #endif diff --git a/token.c b/token.c index 29978ce8..a377b295 100644 --- a/token.c +++ b/token.c @@ -117,7 +117,7 @@ send_deflated_token(int f, int token, tx_strm.zfree = z_free; if (deflateInit2(&tx_strm, Z_DEFAULT_COMPRESSION, 8, -15, 8, Z_DEFAULT_STRATEGY) != Z_OK) { - fprintf(FERROR, "compression init failed\n"); + rprintf(FERROR, "compression init failed\n"); exit_cleanup(1); } if ((obuf = malloc(MAX_DATA_COUNT+2)) == NULL) @@ -168,7 +168,7 @@ send_deflated_token(int f, int token, } r = deflate(&tx_strm, nb? Z_NO_FLUSH: Z_PACKET_FLUSH); if (r != Z_OK) { - fprintf(FERROR, "deflate returned %d\n", r); + rprintf(FERROR, "deflate returned %d\n", r); exit_cleanup(1); } if (nb == 0 || tx_strm.avail_out == 0) { @@ -191,7 +191,7 @@ send_deflated_token(int f, int token, tx_strm.avail_out = 2 * toklen; r = deflate(&tx_strm, Z_INSERT_ONLY); if (r != Z_OK || tx_strm.avail_in != 0) { - fprintf(FERROR, "deflate on token returned %d (%d bytes left)\n", + rprintf(FERROR, "deflate on token returned %d (%d bytes left)\n", r, tx_strm.avail_in); exit_cleanup(1); } @@ -231,7 +231,7 @@ recv_deflated_token(int f, char **data) rx_strm.zalloc = z_alloc; rx_strm.zfree = z_free; if (inflateInit2(&rx_strm, -15) != Z_OK) { - fprintf(FERROR, "inflate init failed\n"); + rprintf(FERROR, "inflate init failed\n"); exit_cleanup(1); } if ((cbuf = malloc(MAX_DATA_COUNT)) == NULL @@ -268,7 +268,7 @@ recv_deflated_token(int f, char **data) r = inflate(&rx_strm, Z_PACKET_FLUSH); n = CHUNK_SIZE - rx_strm.avail_out; if (r != Z_OK) { - fprintf(FERROR, "inflate flush returned %d (%d bytes)\n", + rprintf(FERROR, "inflate flush returned %d (%d bytes)\n", r, n); exit_cleanup(1); } @@ -308,7 +308,7 @@ recv_deflated_token(int f, char **data) r = inflate(&rx_strm, Z_NO_FLUSH); n = CHUNK_SIZE - rx_strm.avail_out; if (r != Z_OK) { - fprintf(FERROR, "inflate returned %d (%d bytes)\n", r, n); + rprintf(FERROR, "inflate returned %d (%d bytes)\n", r, n); exit_cleanup(1); } if (rx_strm.avail_in == 0) @@ -341,7 +341,7 @@ see_deflate_token(char *buf, int len) rx_strm.avail_in = len; r = inflateIncomp(&rx_strm); if (r != Z_OK) { - fprintf(FERROR, "inflateIncomp returned %d\n", r); + rprintf(FERROR, "inflateIncomp returned %d\n", r); exit_cleanup(1); } } diff --git a/util.c b/util.c index 357b8308..59f94182 100644 --- a/util.c +++ b/util.c @@ -89,7 +89,7 @@ char *map_ptr(struct map_struct *map,OFF_T offset,int len) if (do_lseek(map->fd,offset,SEEK_SET) != offset || (nread=read(map->fd,map->p,len)) != len) { - fprintf(FERROR,"EOF in map_ptr! (offset=%d len=%d nread=%d errno=%d)\n", + rprintf(FERROR,"EOF in map_ptr! (offset=%d len=%d nread=%d errno=%d)\n", (int)offset, len, nread, errno); exit_cleanup(1); } @@ -121,14 +121,14 @@ int piped_child(char **command,int *f_in,int *f_out) if (pipe(to_child_pipe) < 0 || pipe(from_child_pipe) < 0) { - fprintf(FERROR,"pipe: %s\n",strerror(errno)); + rprintf(FERROR,"pipe: %s\n",strerror(errno)); exit_cleanup(1); } pid = do_fork(); if (pid < 0) { - fprintf(FERROR,"fork: %s\n",strerror(errno)); + rprintf(FERROR,"fork: %s\n",strerror(errno)); exit_cleanup(1); } @@ -139,21 +139,21 @@ int piped_child(char **command,int *f_in,int *f_out) close(to_child_pipe[1]) < 0 || close(from_child_pipe[0]) < 0 || dup2(from_child_pipe[1], STDOUT_FILENO) < 0) { - fprintf(FERROR,"Failed to dup/close : %s\n",strerror(errno)); + rprintf(FERROR,"Failed to dup/close : %s\n",strerror(errno)); exit_cleanup(1); } if (to_child_pipe[0] != STDIN_FILENO) close(to_child_pipe[0]); if (from_child_pipe[1] != STDOUT_FILENO) close(from_child_pipe[1]); umask(orig_umask); execvp(command[0], command); - fprintf(FERROR,"Failed to exec %s : %s\n", + rprintf(FERROR,"Failed to exec %s : %s\n", command[0],strerror(errno)); exit_cleanup(1); } if (close(from_child_pipe[1]) < 0 || close(to_child_pipe[0]) < 0) { - fprintf(FERROR,"Failed to close : %s\n",strerror(errno)); + rprintf(FERROR,"Failed to close : %s\n",strerror(errno)); exit_cleanup(1); } @@ -171,14 +171,14 @@ int local_child(int argc, char **argv,int *f_in,int *f_out) if (pipe(to_child_pipe) < 0 || pipe(from_child_pipe) < 0) { - fprintf(FERROR,"pipe: %s\n",strerror(errno)); + rprintf(FERROR,"pipe: %s\n",strerror(errno)); exit_cleanup(1); } pid = do_fork(); if (pid < 0) { - fprintf(FERROR,"fork: %s\n",strerror(errno)); + rprintf(FERROR,"fork: %s\n",strerror(errno)); exit_cleanup(1); } @@ -193,17 +193,17 @@ int local_child(int argc, char **argv,int *f_in,int *f_out) close(to_child_pipe[1]) < 0 || close(from_child_pipe[0]) < 0 || dup2(from_child_pipe[1], STDOUT_FILENO) < 0) { - fprintf(FERROR,"Failed to dup/close : %s\n",strerror(errno)); + rprintf(FERROR,"Failed to dup/close : %s\n",strerror(errno)); exit_cleanup(1); } if (to_child_pipe[0] != STDIN_FILENO) close(to_child_pipe[0]); if (from_child_pipe[1] != STDOUT_FILENO) close(from_child_pipe[1]); - start_server(argc, argv); + start_server(STDIN_FILENO, STDOUT_FILENO, argc, argv); } if (close(from_child_pipe[1]) < 0 || close(to_child_pipe[0]) < 0) { - fprintf(FERROR,"Failed to close : %s\n",strerror(errno)); + rprintf(FERROR,"Failed to close : %s\n",strerror(errno)); exit_cleanup(1); } @@ -217,13 +217,13 @@ int local_child(int argc, char **argv,int *f_in,int *f_out) void out_of_memory(char *str) { - fprintf(FERROR,"ERROR: out of memory in %s\n",str); + rprintf(FERROR,"ERROR: out of memory in %s\n",str); exit_cleanup(1); } void overflow(char *str) { - fprintf(FERROR,"ERROR: buffer overflow in %s\n",str); + rprintf(FERROR,"ERROR: buffer overflow in %s\n",str); exit_cleanup(1); } @@ -370,20 +370,20 @@ int copy_file(char *source, char *dest, mode_t mode) ifd = open(source, O_RDONLY); if (ifd == -1) { - fprintf(FERROR,"open %s: %s\n", + rprintf(FERROR,"open %s: %s\n", source,strerror(errno)); return -1; } if (do_unlink(dest) && errno != ENOENT) { - fprintf(FERROR,"unlink %s: %s\n", + rprintf(FERROR,"unlink %s: %s\n", dest,strerror(errno)); return -1; } ofd = do_open(dest, O_WRONLY | O_CREAT | O_TRUNC | O_EXCL, mode); if (ofd < 0) { - fprintf(FERROR,"open %s: %s\n", + rprintf(FERROR,"open %s: %s\n", dest,strerror(errno)); close(ifd); return -1; @@ -391,7 +391,7 @@ int copy_file(char *source, char *dest, mode_t mode) while ((len = safe_read(ifd, buf, sizeof(buf))) > 0) { if (full_write(ofd, buf, len) < 0) { - fprintf(FERROR,"write %s: %s\n", + rprintf(FERROR,"write %s: %s\n", dest,strerror(errno)); close(ifd); close(ofd); @@ -403,7 +403,7 @@ int copy_file(char *source, char *dest, mode_t mode) close(ofd); if (len < 0) { - fprintf(FERROR,"read %s: %s\n", + rprintf(FERROR,"read %s: %s\n", source,strerror(errno)); return -1; } @@ -445,3 +445,59 @@ void kill_all(int sig) kill(all_pids[i], sig); } } + +/* this is the rsync debugging function. Call it with FINFO or FERROR */ +void rprintf(int fd, const char *format, ...) +{ + va_list ap; + char buf[1024]; + int len; + FILE *f=NULL; + + va_start(ap, format); + +#if HAVE_VSNPRINTF + len = vsnprintf(buf, sizeof(buf)-1, format, ap); +#else + len = vsprintf(buf, format, ap); +#endif + va_end(ap); + + if (len < 0) exit_cleanup(1); + + if (fd == FERROR) { + f = stderr; + } + + if (fd == FINFO) { + extern int am_server; + if (am_server) + f = stderr; + else + f = stdout; + } + + if (!f) exit_cleanup(1); + + if (fwrite(buf, len, 1, f) != 1) exit_cleanup(1); +} + +void rflush(int fd) +{ + FILE *f = NULL; + + if (fd == FERROR) { + f = stderr; + } + + if (fd == FINFO) { + extern int am_server; + if (am_server) + f = stderr; + else + f = stdout; + } + + if (!f) exit_cleanup(1); + fflush(f); +} -- 2.34.1