+ STRUCT_STAT st;
+ int e;
+
+ if (verbose > 2)
+ rprintf(FINFO,"get_local_name count=%d %s\n",
+ flist->count, NS(name));
+
+ if (!name)
+ return NULL;
+
+ if (do_stat(name,&st) == 0) {
+ if (S_ISDIR(st.st_mode)) {
+ if (!push_dir(name)) {
+ rprintf(FERROR, "push_dir %s failed: %s (1)\n",
+ full_fname(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 && ((e = strlen(name)) <= 1 || name[e-1] != '/'))
+ return name;
+
+ if (do_mkdir(name,0777 & ~orig_umask) != 0) {
+ rprintf(FERROR, "mkdir %s failed: %s\n",
+ full_fname(name), strerror(errno));
+ exit_cleanup(RERR_FILEIO);
+ } else {
+ if (verbose > 0)
+ rprintf(FINFO,"created directory %s\n",name);
+ }
+
+ if (!push_dir(name)) {
+ rprintf(FERROR, "push_dir %s failed: %s (2)\n",
+ full_fname(name), strerror(errno));
+ exit_cleanup(RERR_FILESELECT);
+ }
+
+ return NULL;
+}
+
+
+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)
+ rprintf(FINFO,"server_sender starting pid=%d\n",(int)getpid());
+
+ if (!relative_paths && !push_dir(dir)) {
+ rprintf(FERROR, "push_dir %s failed: %s (3)\n",
+ full_fname(dir), strerror(errno));
+ exit_cleanup(RERR_FILESELECT);
+ }
+ argc--;
+ argv++;
+
+ if (strcmp(dir,".")) {
+ int l = strlen(dir);
+ if (strcmp(dir,"/") == 0)
+ l = 0;
+ for (i=0;i<argc;i++)
+ argv[i] += l+1;
+ }
+
+ if (argc == 0 && recurse) {
+ argc=1;
+ argv--;
+ argv[0] = ".";
+ }
+
+ flist = send_file_list(f_out,argc,argv);
+ if (!flist || flist->count == 0) {
+ exit_cleanup(0);
+ }
+
+ io_start_buffering_in(f_in);
+ io_start_buffering_out(f_out);
+ send_files(flist,f_out,f_in);
+ io_flush(FULL_FLUSH);
+ report(f_out);
+ if (protocol_version >= 24) {
+ /* final goodbye message */
+ read_int(f_in);
+ }
+ io_flush(FULL_FLUSH);
+ exit_cleanup(0);
+}
+
+
+static int do_recv(int f_in,int f_out,struct file_list *flist,char *local_name)
+{
+ int pid;
+ int status=0;
+ int error_pipe[2];
+
+ if (preserve_hard_links)
+ init_hard_links(flist);
+
+ if (!delete_after) {
+ /* I moved this here from recv_files() to prevent a race condition */
+ if (recurse && delete_mode && !local_name && flist->count>0) {
+ delete_files(flist);
+ }
+ }
+
+ if (fd_pair(error_pipe) < 0) {
+ rprintf(FERROR,"error pipe failed in do_recv\n");
+ exit_cleanup(RERR_SOCKETIO);
+ }
+
+ io_flush(NORMAL_FLUSH);
+
+ if ((pid=do_fork()) == 0) {
+ close(error_pipe[0]);
+ if (f_in != f_out) close(f_out);
+
+ /* we can't let two processes write to the socket at one time */
+ io_multiplexing_close();
+
+ /* set place to send errors */
+ set_msg_fd_out(error_pipe[1]);
+
+ recv_files(f_in,flist,local_name);
+ io_flush(FULL_FLUSH);
+ report(f_in);
+
+ send_msg(MSG_DONE, "", 0);
+ io_flush(FULL_FLUSH);
+ /* finally we go to sleep until our parent kills us
+ * with a USR2 signal. We sleep for a short time as on
+ * some OSes a signal won't interrupt a sleep! */
+ while (1)
+ msleep(20);
+ }
+
+ close(error_pipe[1]);
+ if (f_in != f_out) close(f_in);
+
+ io_start_buffering_out(f_out);
+
+ set_msg_fd_in(error_pipe[0]);
+
+ generate_files(f_out, flist, local_name);
+
+ get_redo_num(); /* Read final MSG_DONE and any prior messages. */
+ io_flush(FULL_FLUSH);
+ if (protocol_version >= 24) {
+ /* send a final goodbye message */
+ write_int(f_out, -1);
+ }
+ io_flush(FULL_FLUSH);
+
+ set_msg_fd_in(-1);
+ kill(pid, SIGUSR2);
+ wait_process(pid, &status);
+ return status;
+}
+
+
+static void do_server_recv(int f_in, int f_out, int argc,char *argv[])
+{
+ int status;
+ struct file_list *flist;
+ char *local_name=NULL;
+ char *dir = NULL;
+
+ if (verbose > 2)
+ rprintf(FINFO,"server_recv(%d) starting pid=%d\n",argc,(int)getpid());
+
+ if (am_daemon && lp_read_only(module_id) && !am_sender) {
+ rprintf(FERROR,"ERROR: module is read only\n");
+ exit_cleanup(RERR_SYNTAX);
+ return;
+ }
+
+
+ if (argc > 0) {
+ dir = argv[0];
+ argc--;
+ argv++;
+ if (!am_daemon && !push_dir(dir)) {
+ rprintf(FERROR, "push_dir %s failed: %s (4)\n",
+ full_fname(dir), strerror(errno));
+ exit_cleanup(RERR_FILESELECT);
+ }
+ }
+
+ io_start_buffering_in(f_in);
+ if (delete_mode && !delete_excluded)
+ recv_exclude_list(f_in);
+
+ if (filesfrom_fd >= 0) {
+ /* We're receiving the file info from the sender, so we need
+ * the IO routines to automatically write out the names onto
+ * our f_out socket as we read the list info from the sender.
+ * This avoids both deadlock and extra delays/buffers. */
+ io_set_filesfrom_fds(filesfrom_fd, f_out);
+ filesfrom_fd = -1;
+ }
+
+ if (read_batch)
+ flist = batch_flist;
+ else
+ flist = recv_file_list(f_in);
+ if (!flist) {
+ rprintf(FERROR,"server_recv: recv_file_list error\n");
+ exit_cleanup(RERR_FILESELECT);
+ }
+
+ if (argc > 0) {
+ if (strcmp(dir,".")) {
+ argv[0] += strlen(dir);
+ if (argv[0][0] == '/') argv[0]++;
+ }
+ local_name = get_local_name(flist,argv[0]);
+ }
+
+ status = do_recv(f_in,f_out,flist,local_name);
+ exit_cleanup(status);
+}
+
+
+int child_main(int argc, char *argv[])
+{
+ start_server(STDIN_FILENO, STDOUT_FILENO, argc, argv);
+ return 0;
+}
+
+
+void start_server(int f_in, int f_out, int argc, char *argv[])
+{
+ setup_protocol(f_out, f_in);
+
+ set_nonblocking(f_in);
+ set_nonblocking(f_out);
+
+ if (protocol_version >= 23)
+ io_start_multiplex_out(f_out);
+
+ if (am_sender) {
+ if (!read_batch) {
+ recv_exclude_list(f_in);
+ if (cvs_exclude)
+ add_cvs_excludes();
+ }
+ do_server_sender(f_in, f_out, argc, argv);
+ } else {
+ do_server_recv(f_in, f_out, argc, argv);
+ }
+ exit_cleanup(0);
+}
+
+
+/*
+ * This is called once the connection has been negotiated. It is used
+ * for rsyncd, remote-shell, and local connections.
+ */
+int client_run(int f_in, int f_out, pid_t pid, int argc, char *argv[])
+{
+ struct file_list *flist = NULL;
+ int status = 0, status2 = 0;
+ char *local_name = NULL;
+
+ cleanup_child_pid = pid;
+ if (read_batch)
+ flist = batch_flist;
+
+ set_nonblocking(f_in);
+ set_nonblocking(f_out);
+
+ setup_protocol(f_out,f_in);
+
+ if (protocol_version >= 23)
+ io_start_multiplex_in(f_in);
+
+ if (am_sender) {
+ io_start_buffering_out(f_out);
+ if (cvs_exclude)
+ add_cvs_excludes();
+ if (delete_mode && !delete_excluded)
+ send_exclude_list(f_out);
+ if (remote_filesfrom_file)
+ filesfrom_fd = f_in;
+ if (!read_batch) /* don't write to pipe */
+ flist = send_file_list(f_out,argc,argv);
+ if (verbose > 3)
+ rprintf(FINFO,"file list sent\n");
+
+ io_flush(NORMAL_FLUSH);
+ send_files(flist,f_out,f_in);
+ io_flush(FULL_FLUSH);
+ if (protocol_version >= 24) {
+ /* final goodbye message */
+ read_int(f_in);
+ }
+ if (pid != -1) {
+ if (verbose > 3)
+ rprintf(FINFO,"client_run waiting on %d\n", (int) pid);
+ io_flush(FULL_FLUSH);
+ wait_process(pid, &status);
+ }
+ report(-1);
+ io_flush(FULL_FLUSH);
+ exit_cleanup(status);
+ }
+
+ if (argc == 0) {
+ list_only = 1;
+ }
+
+ if (!write_batch)
+ send_exclude_list(f_out);
+
+ if (filesfrom_fd >= 0) {
+ io_set_filesfrom_fds(filesfrom_fd, f_out);
+ filesfrom_fd = -1;
+ }
+
+ flist = recv_file_list(f_in);
+ if (!flist || flist->count == 0) {
+ rprintf(FINFO, "client: nothing to do: "
+ "perhaps you need to specify some filenames or "
+ "the --recursive option?\n");
+ exit_cleanup(0);
+ }
+
+ local_name = get_local_name(flist,argv[0]);
+
+ status2 = do_recv(f_in,f_out,flist,local_name);
+
+ if (pid != -1) {
+ if (verbose > 3)
+ rprintf(FINFO,"client_run2 waiting on %d\n", (int) pid);
+ io_flush(FULL_FLUSH);
+ wait_process(pid, &status);
+ }
+
+ return MAX(status, status2);